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的值 / ...
随机推荐
- Problem B: 零起点学算法17——2个数比较大小
#include<stdio.h> int main() { int n,m; while(scanf("%d %d",&n,&m)!=EOF) if( ...
- iOS开发——检测屏幕旋转
步骤一.注册通知 1: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrien ...
- <摘录>Linux下动态共享库加载时的搜索路径详解
对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while loading shared libraries”这样的错误,这是典型的因为需要的动态库不在动态链接器ld.so的搜索路径 ...
- Word绘制跨行表格
如图“用户评价电影数目”,我们需要均分第一行,选中这三个个,设置了表个高度0.5cm,但是发现上面的一个比较考上,我们需要找到水平竖直居中,那么双击表格,打开表格工具,有设计和布局,切换到布局就找到了 ...
- 【js】js中const,var,let区别
在node.js使用例子中,第一次看到const的声明,查询了一下,可以看得出来: http://www.cnblogs.com/ksl666/p/5944718.html 参考 主要内容是:js中三 ...
- SQL Server 笔试题总结
1:编写Sql语句,查询id重复3次以上的条目,表为Pram(id,name) 先看建立的表: SQL语句: 直接使用一个子查询即可 select * from Pram where id in(se ...
- 通过python脚本查看端口
[root@zabbix-server alertscripts]# cat check_port1.py #!/usr/bin/env python #coding:utf-8 import os, ...
- 我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具
用途说明 SWIG是Simplified Wrapper and Interface Generator的缩写,其官方站点是http://www.swig.org/.SWIG是个帮助使用C或者C++编 ...
- Hibernate参数设置一览表
属性名 用途 hibernate.dialect 一个Hibernate Dialect类名允许Hibernate针对特定的关系数据库生成优化的SQL. 取值 full.classname.of.Di ...
- virtualbox虚拟机ubuntu操作系统,设置网络互通、访问,能访问虚拟机swoole的http服务
网络互通 1.设置virtualbox网络连接模式为桥接网卡模式 2.重启虚拟机查看虚拟机IP ifconfig 3.开启window的telnet程序 控制面板->程序->启用或关闭wi ...