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的值 / ...
随机推荐
- csdn下载频道 循环登录的解决方法
一直循环登录,明明登录成功,跳转页面后还是无法下载,还是要登录,一直循环. 解决方法是修改密码.看来asp果然是垃圾啊.
- 10.3(Java学习笔记)JDBC时间操作
一.时间分类 数据库 java类 Date ---- java.sql.Date 表示日期 yyyy-MM--dd (年月日) Time ----java.sql.Time 表示 ...
- Problem K: 数字菱形
#include<stdio.h> int main() { int n,i,j,k,t,x,q,p; while(scanf("%d",&n)!=EOF) ; ...
- Scala实战高手****第15课:Scala类型参数编程实战及Spark源码鉴赏
1.Scala的类和方法.函数都可以是泛型 2.上界:表示泛型的类型必须是某种类型或者其类型的子类,语法:<: ,对类型进行限定 3.下界:表示泛型的类型必须是某种类型或者其类型的父类,语法:& ...
- Word中对象显示不完整
选中上下文字后,右键没有段落,如果是图片的话是有的,那么我们可以点击菜单栏中段落的右下三角,在那设置单倍行距.
- RequireJs 入门
官网:http://www.requirejs.cn/ 使用方法: 1.引入require.js 可以在底部引入: <script type="text/javascript" ...
- CSS3:绘制图形
CSS画图形,基本上所有的实现都是对边框的角度的调整及组合. 以下不包含兼容浏览器的前缀,请使用时在border-radius前加-moz-.-webkit- .... 1.正常得不得了的矩形 .sq ...
- SQL CTE 递归分割以逗号分隔的字符串
)) INSERT INTO @t SELECT 'AAA,BBB,CCC' SELECT * FROM @t ;WITH mycte AS ( ,mend,num FROM @t UNION ALL ...
- javascript函数,构造函数。js对象和json的区别。js中this指向问题
函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块.好处:在出现大量程序相同的时候,可以封装为一个function,这样只用调用一次,就能执行很多语句.(1)语法:函数就是包裹在花括号中的代码 ...
- [Android Memory] Android性能测试小工具Emmagee
转载:http://blog.csdn.net/anlegor/article/details/22895993 Emmagee是网易杭州QA团队开发的用于测试指定android应用性能的小工具.该工 ...