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# 获取或设置本地打印机及配置文件操作的更多相关文章

  1. VB6 获取和设置默认打印机

    Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA&quo ...

  2. e868. 获取和设置本地外观

    By default, Swing uses a cross-platform look and feel called Metal. In most cases, it is more desira ...

  3. windows设置默认打印机

    实现这个功能需要使用windows api [DllImport("winspool.drv")] public static extern bool SetDefaultPrin ...

  4. C#获取本地打印机列表,并将指定打印机设置为默认打印机

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  5. 【VC++技术杂谈002】打印技术之获取及设置系统默认打印机

    本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用EnumPrinters()函数,该函数可以枚举全部的本地.网络打印机信息.其函数原型为: B ...

  6. [WinAPI] API 14 [获取、设置文件属性和时间]

    >_< 为了获取文件属性,用户可以使用GetFileAttributes与GetFileAttributesEx函数. GetFileAttributesEx函数除了返回文件属性外,还返回 ...

  7. C#Winfrom系统打印机调用/设置默认打印机

    实现如下效果: 实现方式如下: using System;using System.Drawing.Printing;using System.Runtime.InteropServices;usin ...

  8. C#设置默认打印机

    项目中,需要选择打印机,切换打印机.demo如下(wpf应用程序): Xaml: <Window x:Class="PrintersApp.MainWindow" xmlns ...

  9. js无法获取.net设置的cookie

    使用CookieHelper帮助类: public class CookieHelper { #region 获取Cookie /// <summary> /// 获得Cookie的值 / ...

随机推荐

  1. Codeforces 785E Anton and Permutation(分块)

    [题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...

  2. 快速创建Django验证码

    # 生成随机验证码图片 import stringfrom random import randint, samplefrom PIL import Image, ImageDraw, ImageFo ...

  3. java static代码段

    1)java中还有个static代码块的形式,形式为 static {……}.static代码块是类定义的一部分,仅仅在类被初次加载的时候被调用一次,之后再调用不会再加载.那么类什么时候首次被加载呢? ...

  4. Linux下date常用格式化输出

    date "+%Y-%m-%d" --date "+%H:%M:%S" ::date "+%Y-%m-%d %H:%M:%S" -- ::d ...

  5. struts-2-spring-2-jpa-ajax

    http://struts.apache.org/docs/struts-2-spring-2-jpa-ajax.html

  6. 网络采集软件核心技术剖析系列(2)---如何使用C#语言获得任意站点博文的正文及标题

    一 本系列随笔概览及产生的背景 本系列开篇受到大家的热烈欢迎,这对博主是莫大的鼓励,此为本系列第二篇,希望大家继续支持,为我继续写作提供动力. 自己开发的豆约翰博客备份专家软件工具问世3年多以来,深受 ...

  7. ubi层次

    转:http://www.360doc.com/content/11/0518/13/496343_117643185.shtml UBI是什么? 它是一种flash管理方式 flash是一系列连续的 ...

  8. 大数据的开始:安装hadoop

    为实现全栈,从今天开始研究Hadoop,个人体会是成为某方面的专家需要从三个方面着手 系统化的知识(需要看书或者比较系统的培训) 碎片化的知识(需要根据关注点具体的深入的了解) 经验的积累(需要遇到问 ...

  9. iOS:socket通信

    ios开发 Socket通信 Socket描述了一个IP.端口对.它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息.所以,Socket一定包含了通 ...

  10. virtualbox 设置windows 于ubuntu虚拟机共享文件夹

    会弹出错误, .. ..还是会有错误, modprobe       -a vboxsf 之后就可以挂载成功了!!