C# 获取NTP远程服务同步时间
C# 获取NTP远程服务同步时间
现场有一个需要定时同步远程服务器的需求,根据一个时钟电脑,同步服务器时间,保证各个系统之间时间的一致性。
C# 获取NTP远程服务同步时间
现场有一个需要定时同步远程服务器的需求,根据一个时钟电脑,同步服务器时间,保证各个系统之间时间的一致性。
开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib,
就可以很容易的实现压缩和解压缩功能。
主要是操作(Zip)压缩算法
上代码:
压缩文件
/// <summary>
/// 压缩指定文件生成ZIP文件
/// </summary>
/// <param name="topDirName">顶层文件夹名称</param>
/// <param name="fileNamesToZip">待压缩文件</param>
/// <param name="ZipedFileName">ZIP文件</param>
/// <param name="CompressionLevel">压缩比</param>
/// <param name="password">密码</param>
/// <param name="comment">压缩文件注释文字</param>
public static void ZipFile
(
string topDirName, //顶层文件夹名称
string fileNameToZip, //待压缩文件
string ZipedFileName, //ZIP文件
int CompressionLevel, //压缩比
string password, //密码
string comment //压缩文件注释文字
)
{
var ls = new List<string> { fileNameToZip };
ZipFile(topDirName, ls.ToArray(), ZipedFileName, CompressionLevel, password, comment);
}
/// <summary>
/// 压缩指定多个文件生成ZIP文件
/// </summary>
/// <param name="topDirName">顶层文件夹名称</param>
/// <param name="fileNamesToZip">待压缩文件列表</param>
/// <param name="ZipedFileName">ZIP文件</param>
/// <param name="CompressionLevel">压缩比</param>
/// <param name="password">密码</param>
/// <param name="comment">压缩文件注释文字</param>
public static void ZipFile
(
string topDirName, //顶层文件夹名称
string[] fileNamesToZip, //待压缩文件列表
string ZipedFileName, //ZIP文件
int CompressionLevel, //压缩比 9
string password, //密码 ""
string comment //压缩文件注释文字 ""
)
{
using (var s = new ZipOutputStream(File.Open(ZipedFileName, FileMode.Create)))
{
if (password != null && password.Length > 0)
s.Password = password;
if (comment != null && comment.Length > 0)
s.SetComment(comment);
s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression
foreach (string file in fileNamesToZip)
{
using (FileStream fs = File.OpenRead(topDirName + file))
{ //打开待压缩文件
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length); //读取文件流
ZipEntry entry = new ZipEntry(file); //新建实例
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
s.Finish();
}
}解压缩文件:
C# 自动注册OCX方法
C#开发系统时,有时候会遇到调用其他语言开发的模块。如果对方提供了OCX时,就需要注册使用,但是实时时,每个客户端都注册一遍就比较麻烦。所以需要系统第一次启动时自动注册OCX。
转义字符是以‘\’为开头的字符,后面跟一个或几个字符,其意思是将反斜杠‘\’后面的字符转变成为另外的意思。
字符串中常见的的特殊字符
WPF编程,TextBox支持回车换行以及滚动条的设置方法
设置换行:
TextBox的AcceptsReturn属性设置设为True,
将 TextWrapping 属性设置为 Wrap 会导致输入的文本在到达 TextBox 控件的边缘时换至新行,必要时会自动扩展
TextBox 控件以便为新行留出空间。
C#获取特定进程CPU和内存使用率
首先是获取特定进程对象,可以使用Process.GetProcesses()方法来获取系统中运行的所有进程,或者使用Process.GetCurrentProcess()方法来获取当前程序所对应的进程对象。当有了进程对象后,可以通过进程对象名称来创建PerformanceCounter类型对象,通过设定PerformanceCounter构造函数的参数实现获取特定进程的CPU和内存使用情况。
C# 实现键盘钩子,在搜狗输入法使用中,获取键盘输入
百度百科:解释
Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的。而钩子是Windows系统中非常重要的系统接口,用它可以截获并处理送给其他应用程序的消息,来完成普通应用程序难以实现的功能。
钩子可以监视系统或进程中的各种事件消息,截获发往目标窗口的消息并进行处理。这样,我们就可以在系统中安装自定义的钩子,监视系统中特定事件的发生,完成特定的功能,比如截获键盘、鼠标的输入,屏幕取词,日志监视等等。
记录一个C#语言文件操作类
主要功能:
1、压缩和解压字符串 2、字符串数组转换
FileHelper.cs
/// <summary>
/// 文件帮助类
/// </summary>
public static class FileHelper
{
#region 压缩和解压字符串
/// <summary>
/// 将传入字符串以GZip算法压缩后,返回Base64编码字符
/// </summary>
/// <param name="rawString">需要压缩的字符串</param>
/// <returns>压缩后的Base64编码的字符串</returns>
public static string GZipCompressString(string rawString)
{
if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
{
return "";
}
else
{
byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
byte[] zippedData = Compress(rawData);
return (string)(Convert.ToBase64String(zippedData));
}
}
/// <summary>
/// GZip压缩
/// </summary>
/// <param name="rawData"></param>
/// <returns></returns>
public static byte[] Compress(byte[] rawData)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream compressedzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
compressedzipStream.Write(rawData, 0, rawData.Length);
compressedzipStream.Close();
return ms.ToArray();
}
/// <summary>
/// 解压Sring
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static string GetStringByString(string Value)
{
string CC = GZipDecompressString(Value);
return CC;
}
/// <summary>
/// 将传入的二进制字符串资料以GZip算法解压缩
/// </summary>
/// <param name="zippedString">经GZip压缩后的二进制字符串</param>
/// <returns>原始未压缩字符串</returns>
public static string GZipDecompressString(string zippedString)
{
if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0)
{
return "";
}
else
{
byte[] zippedData = Convert.FromBase64String(zippedString.ToString());
return (string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData)));
}
}
/// <summary>
/// ZIP解压
/// </summary>
/// <param name="zippedData"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] zippedData)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(zippedData);
System.IO.Compression.GZipStream compressedzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
System.IO.MemoryStream outBuffer = new System.IO.MemoryStream();
byte[] block = new byte[1024];
while (true)
{
int bytesRead = compressedzipStream.Read(block, 0, block.Length);
if (bytesRead <= 0)
break;
else
outBuffer.Write(block, 0, bytesRead);
}
compressedzipStream.Close();
return outBuffer.ToArray();
}
#endregion
/// <summary>
/// 文章转换base64字符串
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string BaseStrFromFile(string path)
{
if (File.Exists(path) == false)
return string.Empty;
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
string basestr = string.Empty;
fs.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[fs.Length];
int log = Convert.ToInt32(fs.Length);
fs.Read(data, 0, log);
basestr = Convert.ToBase64String(data);
fs.Close();
return basestr;
}
}
catch (Exception ex)
{
Logger.logError(ex);
return string.Empty;
}
}
/// <summary>
/// 文章转换byte数组
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static byte[] ByteFromFileData(string path)
{
if (File.Exists(path) == false)
return null;
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// string basestr = string.Empty;
fs.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[fs.Length];
int log = Convert.ToInt32(fs.Length);
fs.Read(data, 0, log);
//basestr = Convert.ToBase64String(data);
return data;
}
}
catch (Exception ex)
{
Logger.logError(ex);
return null;
}
}
/// <summary>
/// Base64字符串转文件并保存
/// </summary>
/// <param name="data">base64字符串</param>
/// <param name="fileName">保存的文件名</param>
/// <returns>是否转换并保存成功</returns>
public static bool ByteToFile(byte[] data, string fileFullPath)
{
bool opResult = false;
try
{
if (data != null && data.Length > 0)
{
string strDate = DateTime.Now.ToString("yyyyMMdd");
using (FileStream fs = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
fs.Write(data, 0, data.Length);
fs.Close();
}
}
opResult = true;
}
catch (Exception e)
{
Logger.logError("异常类型: \t" + e.GetType());
Logger.logError("异常描述:\t" + e.Message);
Logger.logError("异常方法:\t" + e.TargetSite);
Logger.logError("异常堆栈:\t" + e.StackTrace);
}
return opResult;
}
/// <summary>
/// Base64字符串转文件并保存
/// </summary>
/// <param name="base64String">base64字符串</param>
/// <param name="fileName">保存的文件名</param>
/// <returns>是否转换并保存成功</returns>
public static bool Base64StringToFile(string base64String, string fileFullPath)
{
bool opResult = false;
try
{
string strDate = DateTime.Now.ToString("yyyyMMdd");
string strbase64 = base64String.Trim().Substring(base64String.IndexOf(",") + 1); //将‘,’以前的多余字符串删除
MemoryStream stream = new MemoryStream(Convert.FromBase64String(strbase64));
using (FileStream fs = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] b = stream.ToArray();
fs.Write(b, 0, b.Length);
fs.Close();
}
opResult = true;
}
catch (Exception e)
{
Logger.logError("异常类型: \t" + e.GetType());
Logger.logError("异常描述:\t" + e.Message);
Logger.logError("异常方法:\t" + e.TargetSite);
Logger.logError("异常堆栈:\t" + e.StackTrace);
}
return opResult;
}
}WPF中 List<T>与 ObservableCollection<T>的 区别
C#中 List<T>与ObservableCollection<T>的用法基本上是一样的。
都是继承 IEnumerable<T>等基本接口
主要区别:
list<T>:
C# 设置开机自动启动方法
我们做系统时,有时候需要开机启动,下面就是开机启动方法:
/// <summary>
/// 设置开机启动
/// </summary>
/// <param name="started">是否开机启动</param>
/// <param name="exeName">程序名称</param>
/// <param name="path">程序路径</param>
/// <returns></returns>
public static bool SetAutoStart(bool started, string exeName, string path)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)
{
try
{
key.SetValue(exeName, path);//设置为开机启动
key.Close();
}
catch
{
return false;
}
}
else
{
try
{
key.DeleteValue(exeName);//取消开机启动
key.Close();
}
catch
{
return false;
}
}
return true;
}直接调用就可以了,会在注册表写入开机启动
wpf 有时候有多绑定的需求
需要多绑定MultiBinding 节点报告,如
<Style x:Key="Color_Patient" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource ColorEConvert}" Mode="TwoWay">
<Binding Path="DataItem.ADM_ID_ISS"></Binding>
<Binding Path="DataItem.ISEXIGENCE"></Binding>
<Binding Path="DataItem.REQ_SERVICE"></Binding>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="{Binding Path=DataItem.REMARK,Converter={StaticResource ColorFConvert}, Mode=TwoWay}"/>
</Style>C# 11.0 的主要特性
1、C# 11 预览:允许在内插字符串的“插值表达式”中换行
C# 支持两种样式的内插字符串:逐字和非逐字内插字符串。它们之间的主要区别在于,非逐字内插字符串不能在其文本段中包含换行符,而必须改用转义(如 \r\n)。逐字内插字符串可以在其文本段中包含换行符,并且不转义换行符或其他字符(""除外,用于转义引号本身)。
C# 10 完整特性介绍
.NET 的基础库、语言、运行时团队从来都是相互独立各自更新的,.NET 6 在基础库、运行时上同样做了非常多的改进,本文仅仅介绍语言部分。
距离上次介绍 C# 10 的特性已经有一段时间了,伴随着 .NET 6 的开发进入尾声,C# 10 最终的特性也终于敲定了。总的来说 C# 10 的更新内容很多,并且对类型系统做了不小的改动,解决了非常多现有的痛点。
C# 9.0 的主要特性
1.顶级语句
使用C#8.0中的模式
C# 7.0 的主要特性
C#7集成到 .NET Framework4.6.2和Visual Studio2017中,增加了元组和模式匹配,使得C#更具函数式语言特点
<RichTextBox Name="rtbTest" > <!--设置行间距--> <RichTextBox.Document> <FlowDocument LineHeight="5" > <Paragraph/> </FlowDocument> </FlowDocument> </RichTextBox.Document> </RichTextBox>
IIS 运行网站提示:
HTTP 错误 500.21 - Internal Server Error
处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler”