C# 获取或设置本地打印机及配置文件操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Drawing; namespace Demo
{
public class SettingConfiguration
{
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(String Name); //调用WinAPI将指定名称的打印机设置为默认打印机 private static PrintDocument fPrintDocument = new PrintDocument();
/// <summary>
/// 获取本机默认打印机名称
/// </summary>
public static string DefaultPrinter
{
get
{
return fPrintDocument.PrinterSettings.PrinterName;
}
} private static string printerName = DefaultPrinter;
/// <summary>
/// 获取或设置打印机,默认获取本机默认打印机
/// </summary>
public static string PrinterName
{
get { return printerName; }
set { printerName = value; }
} /// <summary>
/// 获取本机的打印机列表。列表中的第一项就是默认打印机。
/// </summary>
public static List<String> GetLocalPrinters()
{
try
{
List<String> fPrinters = new List<string>();
fPrinters.Add(DefaultPrinter); // 默认打印机始终出现在列表的第一项
foreach (String fPrinterName in PrinterSettings.InstalledPrinters)
{
if (!fPrinters.Contains(fPrinterName))
fPrinters.Add(fPrinterName);
}
return fPrinters;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 获取指定节点的配置信息
/// </summary>
/// <param name="configName"></param>
/// <returns></returns>
public static string GetConfiguration(string configName)
{
string configVaule = ConfigurationManager.AppSettings[configName];
if (configVaule != null && configVaule != "")
{
return configVaule.ToString();
}
return "";
} /// <summary>
/// 判断是否存在节点
/// </summary>
/// <param name="configName"></param>
/// <returns></returns>
public static bool ConfigExists(string configName)
{
string configVaule = ConfigurationManager.AppSettings[configName];
if (configVaule == null)
{
return false;
}
return true;
} /// <summary>
/// 移除指定的 app.config 节点
/// </summary>
/// <param name="configName"></param>
public static void RemoveConfigurationNode(string configName)
{
try
{
ConfigurationManager.AppSettings.Remove(configName);
ConfigurationManager.RefreshSection("appSettings");// 刷新命名节,在下次检索它时将从磁盘重新读取它。
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
///
/// </summary>
/// <param name="SetModel">Add/set</param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetConfiguration(string key, string value)
{
try
{
//更新配置文件:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
bool setModel = ConfigExists(key);
if (setModel)
{
//config.AppSettings.Settings.Remove(key);
////添加
//config.AppSettings.Settings.Add(key, value);
//修改
config.AppSettings.Settings[key].Value = value;
}
else
{
//添加
config.AppSettings.Settings.Add(key, value);
}
//最后调用
config.Save(ConfigurationSaveMode.Modified);
//当前的配置文件更新成功。
ConfigurationManager.RefreshSection("appSettings");// 刷新命名节,在下次检索它时将从磁盘重新读取它。记住应用程序要刷新节点
}
catch (Exception ex)
{
throw ex;
}
} public static string GetAppPath()
{
try
{
string appPath = System.Windows.Forms.Application.StartupPath.ToLower();
appPath = appPath.Replace("\\Debug", "");
appPath = appPath.Replace("\\Release", "");
appPath = appPath.Replace("bin", "");
return appPath;
}
catch (Exception)
{
throw;
}
} /// <summary>
/// 实现byte[]转换成十六进制String
/// </summary>
/// <param name="arrByte"></param>
/// <returns></returns>
public static string ByteToString(byte[] arrByte)
{
StringBuilder sb = new StringBuilder();
foreach (byte by in arrByte)
{
sb.Append(by > ? Convert.ToString(by, ) : '' + Convert.ToString(by, ));
}
return sb.ToString();
} /// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name="timeStamp">Unix时间戳格式</param>
/// <returns>C#格式时间</returns>
public static DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
long lTime = long.Parse(timeStamp + "");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
} /// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time"> DateTime时间格式</param>
/// <returns>Unix时间戳格式</returns>
public static int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , ));
return (int)(time - startTime).TotalSeconds;
} /// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int ConvertString16ToNumber(string value)
{
return Int32.Parse(value, System.Globalization.NumberStyles.HexNumber);
} /// <summary>
/// 分割字符串
/// </summary>
/// <param name="strOld"></param>
/// <param name="splitChar"></param>
/// <returns></returns>
public static string[] SplitValue(string strOld, char splitChar)
{
try
{
string[] strNew = strOld.Split(splitChar);
return strNew;
}
catch (Exception)
{
throw;
}
}
}
}
C# 获取或设置本地打印机及配置文件操作的更多相关文章
- VB6 获取和设置默认打印机
Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA&quo ...
- e868. 获取和设置本地外观
By default, Swing uses a cross-platform look and feel called Metal. In most cases, it is more desira ...
- windows设置默认打印机
实现这个功能需要使用windows api [DllImport("winspool.drv")] public static extern bool SetDefaultPrin ...
- C#获取本地打印机列表,并将指定打印机设置为默认打印机
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 【VC++技术杂谈002】打印技术之获取及设置系统默认打印机
本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用EnumPrinters()函数,该函数可以枚举全部的本地.网络打印机信息.其函数原型为: B ...
- [WinAPI] API 14 [获取、设置文件属性和时间]
>_< 为了获取文件属性,用户可以使用GetFileAttributes与GetFileAttributesEx函数. GetFileAttributesEx函数除了返回文件属性外,还返回 ...
- C#Winfrom系统打印机调用/设置默认打印机
实现如下效果: 实现方式如下: using System;using System.Drawing.Printing;using System.Runtime.InteropServices;usin ...
- C#设置默认打印机
项目中,需要选择打印机,切换打印机.demo如下(wpf应用程序): Xaml: <Window x:Class="PrintersApp.MainWindow" xmlns ...
- js无法获取.net设置的cookie
使用CookieHelper帮助类: public class CookieHelper { #region 获取Cookie /// <summary> /// 获得Cookie的值 / ...
随机推荐
- [BZOJ3894]文理分科(最小割)
(1) 对每个位置建一个点F1,S向这个点连art[i][j]的边,这个点向T连science[i][j]的边. (2) 对每个位置再建一个点F2,S向这个点连same_art[i][j]的边,这个点 ...
- 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers
线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...
- 【后缀数组】poj3581 Sequence
考虑第一次切割,必然切割的是翻转后字典序最小的前缀,伪证: 若切割位置更靠前:则会导致第一个数翻转后更靠前,字典序必然更大. 若切割位置更靠后,则显然也会导致字典序更大. ↑,sa即可 对于第二次切割 ...
- [POI2010]Frog
题目大意: 一个数轴上有n个点,现在你要在这些点上跳. 每次跳的时候你只能跳到离这个点第k近的点上,而且要连续跳m次. 问从每一个点出发,最后分别会在哪一个点结束. 思路: 首先可以维护一个大小为k+ ...
- Problem H: 计算数列和2/1,3/2,5/3,8/5......
#include<stdio.h> int main(){ int i,n; scanf("%d",&n); float sum=0.0; ; ; ;i< ...
- TELNET终端类型选项
转:http://www.cnpaf.net/Class/Telnet/200408/5.html 1. 命令名称及编号TERMINAL-TYPE242.命令含义IACWILLTERMINAL-TYP ...
- Merge的山寨版“联机帮助”
IF NOT OBJECT_ID('Demo_AllProducts') IS NULL DROP TABLE Demo_AllProducts GO CREATE TABLE Demo_AllPro ...
- scrapy-splash抓取动态数据例子十五
一.介绍 本例子用scrapy-splash爬取电视之家(http://www.tvhome.com/news/)网站的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信 ...
- 贯通Spark Streaming流计算框架的运行源码
本章节内容: 一.在线动态计算分类最热门商品案例回顾 二.基于案例贯通Spark Streaming的运行源码 先看代码(源码场景:用户.用户的商品.商品的点击量排名,按商品.其点击量排名前三): p ...
- Hadoop之Hbase详解
1.什么是Hbase HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统, hbase是列式的分布式数据库 1.2.HBASE优势: 1)线性扩展,随着数据量增多可以通过节点扩展进行支撑 ...