一、获取运行文件目录相关:

1、//使用AppDomain获取当前目录
string dir = AppDomain.CurrentDomain.BaseDirectory;

2、//使用path获取当前应用程序集的执行的上级目录
string dir1 = Path.GetFullPath("..");

3、//使用path获取当前应用程序集的执行的上上级级目录
string dir2 = Path.GetFullPath(@"..//..");

4、//程序自带(常用)
Application.StartupPath

 

二、进程相关:

1、获取进程并关闭进程
Process[] p = Process.GetProcessesByName(clientName);
foreach (Process ps in p)
{
ps.Kill();
}


2、根据路径打开进程
ProcessStartInfo pinfo = new ProcessStartInfo();
pinfo.UseShellExecute = true;
pinfo.FileName = string.Format(@"{0}\{1}\{2}", Application.StartupPath,clientName, fileName); //程序路径
//启动进程
Process p = Process.Start(pinfo);

 

三、序列化与反序列化:

1、json序列化与反序列化
string jsonMsg=JsonConvert.SerializeObject(resetPosition); //序列化对象为json

JObject joStatus = (JObject)JsonConvert.DeserializeObject(jsonMsg); //反序列化为JObject泛型对象

AlarmReportModel alarmReportModel = JsonConvert.DeserializeObject<AlarmReportModel>(jsonMsg); //反序列化为具体对象

2、xml序列化与反序列化


四、开机自启动:

RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
R_run.SetValue("自动升级软件", Application.ExecutablePath);
R_run.Close();
R_local.Close();

 

三、压缩、解压

1、
System.IO.Compression.ZipFile.CreateFromDirectory(@"e:\test", @"e:\test\test.zip"); //压缩
System.IO.Compression.ZipFile.ExtractToDirectory(@"e:\test\test.zip", @"e:\test"); //解压


2、格式支持rar 7zip, zip, tar, tzip和bzip2格式,引用 using SharpCompress.dll
using (Stream stream = File.OpenRead(@"C:\Code\sharpcompress.rar"))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Console.WriteLine(reader.Entry.FilePath);
reader.WriteEntryToDirectory(@"C:\temp");
}
}
}


3、引用 using ICSharpCode.SharpZipLib.Zip
/// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">要压缩的文件</param>
/// <param name="zipedFile">压缩后的文件全名</param>
/// <param name="compressionLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>
/// <param name="blockSize">分块大小</param>
public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
{
if (!System.IO.File.Exists(fileToZip))//如果文件没有找到,则报错
{
throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");
}

FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read);
FileStream zipFile = File.Create(zipedFile);
ZipOutputStream zipStream = new ZipOutputStream(zipFile);
ZipEntry zipEntry = new ZipEntry(fileToZip);
zipStream.PutNextEntry(zipEntry);
zipStream.SetLevel(compressionLevel);
byte[] buffer = new byte[blockSize];
int size = streamToZip.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, size);

try
{
while (size < streamToZip.Length)
{
int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (Exception ex)
{
GC.Collect();
throw ex;
}

zipStream.Finish();
zipStream.Close();
streamToZip.Close();
GC.Collect();
}

 

/// <summary>
/// 解压缩文件(压缩文件中含有子目录)
/// </summary>
/// <param name="zipfilepath">待解压缩的文件路径</param>
/// <param name="unzippath">解压缩到指定目录</param>
/// <returns>解压后的文件列表</returns>
public List<string> UnZip(string zipfilepath, string unzippath)
{
//解压出来的文件列表
List<string> unzipFiles = new List<string>();

//检查输出目录是否以“\\”结尾
if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false)
{
unzippath += "\\";
}

ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(unzippath);
string fileName = Path.GetFileName(theEntry.Name);

//生成解压目录【用户解压到硬盘根目录时,不需要创建】
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(directoryName);
}

if (fileName != String.Empty)
{
//如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入
if (theEntry.CompressedSize == 0)
continue;
//解压文件到指定的目录
directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);
//建立下面的目录和子目录
Directory.CreateDirectory(directoryName);

//记录导出的文件
unzipFiles.Add(unzippath + theEntry.Name);

FileStream streamWriter = File.Create(unzippath + theEntry.Name);

int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
GC.Collect();
return unzipFiles;
}