284 lines
9.2 KiB
C#
284 lines
9.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AllBase.Helper
|
|
{
|
|
public class FileHelper
|
|
{
|
|
/// <summary>
|
|
/// 删除文件夹及子文件内文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
public static void DeleteFolder(string path)
|
|
{
|
|
try
|
|
{
|
|
if (Directory.Exists(path)) //如果存在这个文件夹删除之
|
|
{
|
|
foreach (string d in Directory.GetFileSystemEntries(path))
|
|
{
|
|
if (File.Exists(d))
|
|
File.Delete(d); //直接删除其中的文件
|
|
else
|
|
DeleteFolder(d); //递归删除子文件夹
|
|
}
|
|
Directory.Delete(path, true); //删除已空文件夹
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteWithTime(ex);
|
|
}
|
|
}
|
|
|
|
public static void DeleteFile(string filePath)
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(filePath))
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteWithTime(ex);
|
|
}
|
|
}
|
|
|
|
public static void CopyFolder(string sourceFolder, string destFolder)
|
|
{
|
|
CreateDirectory(destFolder);
|
|
string[] files = Directory.GetFiles(sourceFolder);
|
|
foreach (string file in files)
|
|
{
|
|
string name = Path.GetFileName(file);
|
|
string dest = Path.Combine(destFolder, name);
|
|
File.Copy(file, dest);
|
|
}
|
|
string[] folders = Directory.GetDirectories(sourceFolder);
|
|
foreach (string folder in folders)
|
|
{
|
|
string name = Path.GetFileName(folder);
|
|
string dest = Path.Combine(destFolder, name);
|
|
CopyFolder(folder, dest);
|
|
}
|
|
}
|
|
|
|
public static void MoveFolder(string sourceFolder, string destFolder)
|
|
{
|
|
CreateDirectory(destFolder);
|
|
string[] files = Directory.GetFiles(sourceFolder);
|
|
foreach (string file in files)
|
|
{
|
|
string name = Path.GetFileName(file);
|
|
string dest = Path.Combine(destFolder, name);
|
|
if (File.Exists(dest))
|
|
{
|
|
File.Delete(dest);
|
|
}
|
|
File.Move(file, dest);
|
|
}
|
|
string[] folders = Directory.GetDirectories(sourceFolder);
|
|
foreach (string folder in folders)
|
|
{
|
|
string name = Path.GetFileName(folder);
|
|
string dest = Path.Combine(destFolder, name);
|
|
if (File.Exists(dest))
|
|
{
|
|
File.Delete(dest);
|
|
}
|
|
MoveFolder(folder, dest);
|
|
}
|
|
}
|
|
|
|
public static string Execute(string command, int seconds)
|
|
{
|
|
string output = string.Empty; //输出字符串
|
|
if (command != null && !command.Equals(string.Empty))
|
|
{
|
|
Process process = new Process();//创建进程对象
|
|
ProcessStartInfo startInfo = new ProcessStartInfo();
|
|
startInfo.FileName = "cmd.exe";//设定需要执行的命令
|
|
startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出
|
|
startInfo.UseShellExecute = false;//不使用系统外壳程序启动
|
|
startInfo.RedirectStandardInput = false;//不重定向输入
|
|
startInfo.RedirectStandardOutput = true; //重定向输出
|
|
startInfo.CreateNoWindow = true;//不创建窗口
|
|
process.StartInfo = startInfo;
|
|
try
|
|
{
|
|
if (process.Start())//开始进程
|
|
{
|
|
if (seconds == 0)
|
|
{
|
|
process.WaitForExit();//这里无限等待进程结束
|
|
}
|
|
else
|
|
{
|
|
process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒
|
|
}
|
|
output = process.StandardOutput.ReadToEnd();//读取进程的输出
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);//捕获异常,输出异常信息
|
|
}
|
|
finally
|
|
{
|
|
if (process != null)
|
|
{
|
|
process.Close();
|
|
}
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
public static void CreateDirectory(string dir)
|
|
{
|
|
if (Path.HasExtension(dir))
|
|
{
|
|
dir = Path.GetDirectoryName(dir);
|
|
}
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
}
|
|
|
|
public static string ReadAllText(string filePath, Encoding encoding = null)
|
|
{
|
|
if (encoding == null)
|
|
{
|
|
return File.ReadAllText(filePath, Encoding.UTF8);
|
|
}
|
|
else
|
|
{
|
|
return File.ReadAllText(filePath, encoding);
|
|
}
|
|
}
|
|
|
|
public static void WriteAllText(string filePathOrName, string content, Encoding encoding = null)
|
|
{
|
|
string path = Path.Combine(GetApplicationDir(), filePathOrName);
|
|
CreateDirectory(path);
|
|
if (encoding == null)
|
|
{
|
|
File.WriteAllText(path, content, Encoding.UTF8);
|
|
}
|
|
else
|
|
{
|
|
File.WriteAllText(path, content, encoding);
|
|
}
|
|
}
|
|
|
|
public static void AppendAllText(string filePathOrName, string content, Encoding encoding = null)
|
|
{
|
|
string path = Path.Combine(GetApplicationDir(), filePathOrName);
|
|
CreateDirectory(path);
|
|
List<string> contentList = new List<string> { content };
|
|
if (encoding == null)
|
|
{
|
|
File.AppendAllLines(path, contentList, Encoding.UTF8);
|
|
}
|
|
else
|
|
{
|
|
File.AppendAllLines(path, contentList, encoding);
|
|
}
|
|
}
|
|
|
|
#region 获取软件所在目录
|
|
public static string GetApplicationDir()
|
|
{
|
|
return AppDomain.CurrentDomain.BaseDirectory;
|
|
}
|
|
#endregion
|
|
|
|
public static string GetParentDir(string dir)
|
|
{
|
|
dir = dir.Trim(Path.DirectorySeparatorChar);
|
|
int last = dir.LastIndexOf(Path.DirectorySeparatorChar);
|
|
if (last > 0)
|
|
{
|
|
return dir.Substring(0, last);
|
|
}
|
|
return dir;
|
|
}
|
|
|
|
public static string GetResourceFileContent(Assembly assembly, string resourceName)
|
|
{
|
|
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
|
|
{
|
|
using (StreamReader reader = new StreamReader(stream))
|
|
{
|
|
string result = reader.ReadToEnd();
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Stream GetResourceFileStream(Assembly assembly, string resourceName)
|
|
{
|
|
// 调用之后要释放 stream
|
|
Stream stream = assembly.GetManifestResourceStream(assembly.GetCustomAttribute<AssemblyTitleAttribute>().Title + "." + resourceName);
|
|
return stream;
|
|
}
|
|
|
|
public static void SaveStream(Stream stream, string destPath)
|
|
{
|
|
CreateDirectory(destPath);
|
|
using (var fileStream = new FileStream(destPath, FileMode.Create, FileAccess.Write))
|
|
{
|
|
stream.CopyTo(fileStream);
|
|
}
|
|
}
|
|
|
|
public static long GetDirectorySize(string folderPath)
|
|
{
|
|
DirectoryInfo di = new DirectoryInfo(folderPath);
|
|
if (!di.Exists)
|
|
{
|
|
return 0;
|
|
}
|
|
return di.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
|
|
}
|
|
|
|
public static void OpenUrl(string url)
|
|
{
|
|
try
|
|
{
|
|
Process.Start(url);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.WriteWithTime(ex);
|
|
}
|
|
}
|
|
|
|
public static void OpenDirectory(string dir)
|
|
{
|
|
Process.Start("explorer.exe", dir);
|
|
}
|
|
|
|
public static string GetFileName(string dir)
|
|
{
|
|
string fileName = Path.GetFileName(dir);
|
|
int questionMark = fileName.IndexOf("?");
|
|
if (questionMark > 0)
|
|
{
|
|
fileName = fileName.Substring(0, questionMark);
|
|
}
|
|
return fileName;
|
|
}
|
|
}
|
|
}
|