using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace ZipFileHelper
{
class CommonFunctions
{
#region 单例模式
private static CommonFunctions uniqueInstance;
private static object _lock = new object();
private CommonFunctions() { }
public static CommonFunctions getInstance()
{
if (null == uniqueInstance) //确认要实例化后再进行加锁,降低加锁的性能消耗。
{
lock (_lock)
{
if (null == uniqueInstance)
{
uniqueInstance = new CommonFunctions();
}
}
}
return uniqueInstance;
}
#endregion
#region 进程
///
/// 另起一进程执行命令
///
/// 可执行程序(路径+名)
/// 执行命令
/// 执行所在初始目录
/// 进程窗口样式
/// Shell启动程序
public void ExecuteProcess(string exe, string commandInfo, string workingDir = "", ProcessWindowStyle processWindowStyle = ProcessWindowStyle.Hidden, bool isUseShellExe = false)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = exe;
startInfo.Arguments = commandInfo;
startInfo.WindowStyle = processWindowStyle;
startInfo.UseShellExecute = isUseShellExe;
if (!string.IsNullOrWhiteSpace(workingDir))
{
startInfo.WorkingDirectory = workingDir;
}
ExecuteProcess(startInfo);
}
///
/// 直接另启动一个进程
///
/// 启动进程时使用的一组值
public void ExecuteProcess(ProcessStartInfo startInfo)
{
try
{
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
process.Close();
process.Dispose();
}
catch (Exception ex)
{
throw new Exception("进程执行失败:\n\r" + startInfo.FileName + "\n\r" + ex.Message);
}
}
///
/// 去掉文件夹或文件的只读属性
///
/// 文件夹或文件全路径
public void Attribute2Normal(string objectPathName)
{
if (true == Directory.Exists(objectPathName))
{
System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(objectPathName);
directoryInfo.Attributes = FileAttributes.Normal;
}
else
{
File.SetAttributes(objectPathName, FileAttributes.Normal);
}
}
#endregion
}
}