对文件及文件夹操作总结

项目中有新的需求,用Tree来显示远程服务器相应磁盘的所有目录。并对选定的文件或者文件夹进行操作。对于如何连接远程服务器并读取目录信息并用Tree显示不是这次总结的重点。

对文件或者文件夹的操作包括新建,复制,剪切,删除(这里包括永久删除和删除到回收站)。等等,不是已经有File类和Directory类里面的好多静态方法可以实现上面的功能吗?对,上面提到的静态方法的确已经很大程度的满足了我们的需求,但是。。。

1. 删除操作没有提供显示的确认(当然您可以自己确认),而且该删除操作是永久 删除。 

2. 剪切(也就是对于的Move方法)只能是同一磁盘下面的操作,不能跨盘Move(对于文件夹而言) 

3. 对于复制剪切或者删除多个文件不能显示进度条(像系统一样的进度条)

基于上面的原因我选择了使用Windows Shell里面的API--- SHFileOperation,它包含4个操作:FO_COPY,FO_DELETE,FO_MOVE,FO_RENAME,您可以看这里来了解。对于基本的操作不用多说,这里只是说用到的与C#的IO类库不用的用法。

1. 删除文件的时候确认操作:SHFILEOPSTRUCT里面的成员fFlags默认情况下会有确认操作的提示框,如果您不想要提示框,可以 shf.fFlags =  FOF_NOCONFIRMATION;

2. 删除到回收站和永久删除:shf.fFlags =  FOF_ALLOWUNDO,顾名思义,允许撤销的话将删除到回收站,否则将永久删除。

3. 操作文件或者文件夹的时候显示进度条:shf.fFlags =  FOF_SIMPLEPROGRESS

4. 对于要删除的文件或者文件夹使用“\0\0”结束,即"D:\\New Text Document.txt\0\0"

5. 如果要操作多个文件或者文件夹 ,不同的路径用“\0”间隔,即"D:\\New Text Document.txt\0D:\\New Folder\0\0"

 对于fFlags还有很多,您可以看这里了解。

示例代码如下:

ExpandedBlockStart.gif 示例
  1       public   static   class  FileEx
  2      {
  3          [StructLayout(LayoutKind.Sequential, CharSet  =  CharSet.Auto, Pack  =   1 )]
  4 
  5           public   struct  SHFILEOPSTRUCT
  6          {
  7 
  8               public  IntPtr hwnd;
  9 
 10              [MarshalAs(UnmanagedType.U4)]
 11               public   int  wFunc;
 12 
 13               public   string  pFrom;
 14 
 15               public   string  pTo;
 16 
 17               public   short  fFlags;
 18 
 19              [MarshalAs(UnmanagedType.Bool)]
 20               public   bool  fAnyOperationsAborted;
 21 
 22               public  IntPtr hNameMappings;
 23 
 24               public   string  lpszProgressTitle;
 25 
 26          }
 27 
 28          [DllImport( " shell32.dll " , CharSet  =  CharSet.Auto)]
 29 
 30           static   extern   int  SHFileOperation( ref  SHFILEOPSTRUCT FileOp);
 31 
 32           const   int  FO_RENAME  =   4 ;
 33 
 34           const   int  FO_DELETE  =   3 ;
 35 
 36           const   int  FO_COPY  =   2 ;
 37 
 38           const   int  FO_MOVE  =   1 ;
 39 
 40           const   int  FOF_ALLOWUNDO  =   0x40 ;
 41 
 42           const   int  FOF_NOCONFIRMATION  =   0x10 ;      // Don't     prompt     the     user.;       
 43 
 44           const   int  FOF_SIMPLEPROGRESS  =   0x100 ;
 45 
 46           public   static   void  SendToRecyclyBin( string  path)
 47          {
 48 
 49              SHFILEOPSTRUCT shf  =   new  SHFILEOPSTRUCT();
 50 
 51              shf.wFunc  =  FO_DELETE;
 52 
 53              shf.fFlags  =   FOF_ALLOWUNDO; //  | FOF_NOCONFIRMATION;
 54 
 55              shf.pFrom  =  path;
 56 
 57               // 返回0表示操作成功
 58              SHFileOperation( ref  shf);
 59 
 60          }
 61 
 62           public   static   bool  Copy( string  from,  string  to)
 63          {
 64              SHFILEOPSTRUCT shf  =   new  SHFILEOPSTRUCT();
 65 
 66              shf.wFunc  =  FO_COPY;
 67 
 68              shf.fFlags  =  FOF_ALLOWUNDO;
 69 
 70              shf.pFrom  =  from;
 71 
 72              shf.pTo  =  to;
 73 
 74               return  SHFileOperation( ref  shf)  ==   0 ;
 75 
 76          }
 77 
 78           public   static   bool  Move( string  from,  string  to)
 79          {
 80 
 81              SHFILEOPSTRUCT shf  =   new  SHFILEOPSTRUCT();
 82 
 83              shf.wFunc  =  FO_MOVE;
 84 
 85              shf.fFlags  =  FOF_ALLOWUNDO;
 86 
 87              shf.pFrom  =  from;
 88 
 89              shf.pTo  =  to;
 90 
 91               return  SHFileOperation( ref  shf)  ==   0 ;
 92 
 93          }
 94 
 95           public   static   bool  RENAME( string  from,  string  to)
 96          {
 97 
 98              SHFILEOPSTRUCT shf  =   new  SHFILEOPSTRUCT();
 99 
100              shf.wFunc  =  FO_RENAME;
101 
102              shf.fFlags  =  FOF_ALLOWUNDO;
103 
104              shf.pFrom  =  from;
105 
106              shf.pTo  =  to;
107 
108               return  SHFileOperation( ref  shf)  ==   0 ;
109 
110          }
111      }

 

只是总结而已。。。 

 

 

 

转载于:https://www.cnblogs.com/WillMeng/archive/2010/02/23/File-Directory-Operation.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值