文件操作是开发过程中经常遇到的,如何创建文件、处理文件、保存文件以及读取文件这些工作是每一位开发都必须经历的。
这里围绕C#文件操作内容,我做了一次系统的梳理,主要包括以下几个模块:
1、文件内容操作:文件读写相关
2、文件检测
3、文件及目录创建和删除
4、文件移动
5、文件加密、解密
6、文件上传、下载
7、文件压缩相关
8、文件操作权限相关
本小节实例主要讲述C#实现使用系统WinRar进行压缩和解压缩的方法,详细内容如下:
1、压缩方法
2、解压缩方法
///
/// 压缩
///
/// 要压缩文件路径
/// 要压缩的文件名
/// 压缩的文件路径
public static void EnZip(string path, string rarName, string rarPath)
{// bool flag = false;try{the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");the_Obj = the_Reg.GetValue("");the_rar = the_Obj.ToString();the_Reg.Close();the_rar = the_rar.Substring(1, the_rar.Length - 7);the_Info = " a " + rarPath + " " + path;the_StartInfo = new ProcessStartInfo();the_StartInfo.FileName = the_rar;the_StartInfo.Arguments = the_Info;the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//打包文件存放目录the_StartInfo.WorkingDirectory = rarName;the_Process = new Process();the_Process.StartInfo = the_StartInfo;the_Process.Start();the_Process.WaitForExit();the_Process.Close();}catch (Exception ex){throw new Exception(ex.Message);}
}
压缩参数命令参数如下:
1
压缩即文件夹及其下文件
//the_Info = " a " + rarName + " " + path + " -r";
2
压缩即文件夹及其下文件 设置压缩方式为 .zip
//the_Info = " a -afzip " + rarName + " " + path;
3
压缩文件夹及其下文件 直接设定为free.zip
//the_Info = " a -r " + rarName + " " + path;
4
搬迁压缩即文件夹及其下文件原文件将不存在
//the_Info = " m " + rarName + " " + path;
5
压缩即文件 直接设定为free.zip 只有文件 而没有文件夹
//the_Info = " a -ep " + rarName + " " + path;
6
加密压缩即文件夹及其下文件 密码为123456 注意参数间不要空格
//the_Info = " a -p123456 " + rarName + " " + path;
///
/// 解压缩
///
/// 要解压的文件名
/// 要解压的文件路径
public static void DeZip(string zipname, string zippath)
{try{the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");the_Obj = the_Reg.GetValue("");the_rar = the_Obj.ToString();the_Reg.Close();the_rar = the_rar.Substring(1, the_rar.Length - 7);the_Info = " X " + zipname + " " + zippath;the_StartInfo = new ProcessStartInfo();the_StartInfo.FileName = the_rar;the_StartInfo.Arguments = the_Info;the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;the_Process = new Process();the_Process.StartInfo = the_StartInfo;the_Process.Start();the_Process.WaitForExit();the_Process.Close();}catch (Exception ex){throw new Exception(ex.Message);}
}
#region 私有变量
static String the_rar; //WinRAR.exe 的完整路径
static RegistryKey the_Reg; //注册表键
static Object the_Obj; //键值
static String the_Info; //cmd命令值
static ProcessStartInfo the_StartInfo;
static Process the_Process;
#endregion//64位系统
//此时会提示:未将对象引用设置为对象的实例
//解决办法:修改注册表,添加如下项:
//HKEY_CLASSES_ROOT\Applications\WinRAR.exe\Shell\Open\Command
//值为:"C:\Program Files (x86)\WinRAR\WinRAR.exe" "%1"#region 调用外部RAR解压缩
private static string rarRegPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";