.NET对文件的多种操作

  要提到对文件的操作,那么自然就会有一下几种方式:

    那么首先我肯定要去找到我要操作的文件夹,请看如下代码

    遍历文件夹找到目标文件

        //获取你代码的物理路径(是指到你当前的页面这一级)
string path = this.Request.PhysicalApplicationPath;
//添加具体准确路径
string realpath = path + "Web/DataImport/DataBackup";
//替换路径格式
string newpath = realpath.Replace("\\", "/");
//获取该路径文件及下面的所有文件名并存储到数组中
string[] filename = Directory.GetFiles(newpath);

    此时将该文件下面的所有完整文件路径都保存在数组当中,那么此时如果你想得到该文件下面有多少个文件,是不是直接length=filename.length就可以了

    接下来我想将文件进行复制一份

           复制文件到指定位置

1         //定义复制的存储路径
2 string varToDirectory = path;
3 if (filename.Length > 0)
4 {
5 foreach (string s in filename)
6 {
7 File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\\")));
8 }
9 }


     那么现在我想对这些文件压缩打包,然后下载到本地:

         于是乎,你可以调用这个函数来压缩你的文件并可指定存储路径

 1 //patch文件/文件夹 路径,rarPatch  rar存储路径, rarName保存的文件夹名称
2
3 public void RARsave(string patch, string rarPatch, string rarName)
4 {
5
6 String the_rar;
7
8 RegistryKey the_Reg;
9
10 Object the_Obj;
11
12 String the_Info;
13
14 ProcessStartInfo the_StartInfo;
15
16 Process the_Process;
17
18 try
19 {
20
21 the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");
22
23 the_Obj = the_Reg.GetValue("");
24
25 the_rar = the_Obj.ToString();
26
27 the_Reg.Close();
28
29 the_rar = the_rar.Substring(1, the_rar.Length - 7);
30 if (!Directory.Exists(patch))
31 {
32 Directory.CreateDirectory(patch);
33 }
34
35
36 //the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; \\文件压缩
37
38 the_Info = " a " + rarName + "" + patch + " -r";
39
40 the_StartInfo = new ProcessStartInfo();
41
42 the_StartInfo.FileName = "WinRar";//the_rar;
43
44 the_StartInfo.Arguments = the_Info;
45
46 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
47
48 //打包文件存放目录
49
50 the_StartInfo.WorkingDirectory = rarPatch;
51
52 the_Process = new Process();
53
54 the_Process.StartInfo = the_StartInfo;
55
56 the_Process.Start();
57
58 the_Process.WaitForExit();
59
60 the_Process.Close();
61
62 }
63
64 catch (Exception ex)
65 {
66
67 throw ex;
68
69 }
70
71 }

          哎呀呀,现在文件也操作了,我觉得他没用了,我就想把它给删除了

          对指定的文件删除掉

1        if (filename.Length > 0)
2 {
3 foreach (string s in filename)
4 {
5 File.Delete(s);
6 }
7 }

         嗯,做了这些工作,那么接下来的工作就交给你了,至于你想用那些操作,那就根据你的需要吧!


 

           

转载于:https://www.cnblogs.com/xiaopanlyu/archive/2012/03/24/2415562.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值