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的值 / ...
随机推荐
- java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to sm.dao.UserDao
在Spring中添加事物管理以后出现的问题 源代码 ApplicationContext applicationContext = new ClassPathXmlApplicationContext ...
- 1.1(Spring学习笔记)Spring基础(BeanFactory、ApplicationContext 、依赖注入)
1.准备工作 下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/ 选择需要下载的版本 ...
- STL之vector3
描述 将n个数字输入到vector里,并对其进行从大到小排序并输出. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { vector<int> vec ...
- 原始DAO开发
1,pojo package com.songyan.dao; import com.songyan.pojo.Student; public interface StudentDao { publi ...
- Android开发工具
Android开发工具: AndroidDevTools: 收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android设计规范,免费的设计素材等. ...
- RowState 介绍
RowState 介绍 从不同位置载入 DataRow 后 RowState 的状态修改, 更改, 删除后的 DataRow.RowState 转化使用 AcceptChanges, RejectCh ...
- ActiveMQ实战-集群
原文:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...
- 【FTP】java FTPClient 文件上传内容为空,文件大小为0
问题:如题所述,使用FTPClient上传至FTP服务器, 表现如下:①文件大小为0 ②上传很小的文件,但是要花费很长的时间,20K要花费2分钟甚至更久 ③没有任何的报错,没有任何的乱码 解决方法: ...
- Kubernetes Fluentd+Elasticsearch+Kibana统一日志管理平台搭建的填坑指南
在初步完成Kubernetes集群架构的建立后,通过搭建一些监控组件,我们已经能够实现 图形化的监控每个node,pod的状态信息和资源情况 通过scale进行replicateSet的扩展和伸缩 通 ...
- javascript快速入门3--分支判断与循环
分支结构 单一选择结构(if) 二路选择结构(if/else) 内联三元运算符 ?: 多路选择结构(switch) var condition = true; if (condition) { ale ...