几个有用的方法

08-01-25 14:35   View:844

1、复制或移动文件夹

  ///

  
  ///复制或移动文件夹  
  ///
  
  ///源文件夹  
  ///目标文件夹  
  ///复制还是移动,1为复制,0为移动         
  public static void CopyOrMoveDirectory(string sourcedirname,string destdirname,int p)  
  {   
   if(!Directory.Exists(destdirname))
   {  
    Directory.CreateDirectory(destdirname);
    File.SetAttributes(destdirname,File.GetAttributes(sourcedirname));
    File.SetAttributes(destdirname,FileAttributes.Normal);
   }               
   if(destdirname[destdirname.Length - 1] != Path.DirectorySeparatorChar)
   {
    destdirname = destdirname + Path.DirectorySeparatorChar;
   }   
   string[] files = Directory.GetFiles(sourcedirname);
   foreach(string file in files)
   {  
    try
    {
     string filename = Path.GetFileName(file);
     if(p == 1)
     {
      //复制
      File.Copy(file,destdirname+filename,true);
     }
     else
     {
      //移动
      string newfile = destdirname + GetNewFilePath(file);
      File.Move(file,newfile);
     }    
     File.SetAttributes(destdirname + filename,FileAttributes.Normal);    
     total++;
    }
    catch
    {

    }
   }
   string[] dirs = Directory.GetDirectories(sourcedirname);    
   foreach(string dir in dirs)
   {    
    CopyOrMoveDirectory(dir,destdirname+Path.GetFileName(dir),p);     
   }
   
  }

public static string GetNewFilePath(string oldfile)
  {
   int pos = oldfile.LastIndexOf("\\");
   oldfile = oldfile.Substring(pos+1);
   string ext = oldfile.Substring(oldfile.IndexOf(".")+1);
   string newfile = DateTime.Now.Ticks.ToString()+"."+ext;
   return newfile;
  }

 

2、添加帐户并将帐户赋给User组

DirectoryEntry AD = new DirectoryEntry( "WinNT://" + Environment.MachineName + ",computer");    
   //添加帐户
   DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");
   NewUser.Invoke("SetPassword", new object[] {"#12345Abc"});
   NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"});
   NewUser.CommitChanges();

   //将帐户赋给Users组
   DirectoryEntry grp;
   grp = AD.Children.Find("Users", "group");
   if (grp != null)
   {
    grp.Invoke("Add", new object[] {NewUser.Path.ToString()});    
   }

3、解压缩文件(注意此方法只使用于安装了winrar的机器)

如果没有安装winrar,那么可下载SharpZipLib组件,重新书写一个方法(以后将会推出完整方法)

/// <summary>
  /// 解压缩指定的rar文件。
  /// </summary>
  /// <param name="rarFileToDecompress">rar文件(绝对路径)。</param>
  /// <param name="directoryToSave">解压缩保存的目录。</param>
  /// <param name="deleteRarFile">解压缩后删除rar文件。</param>
  public static void DecompressRAR(string rarFileToDecompress, string directoryToSave, bool deleteRarFile)
  {
   string winrarExe = @"C:\Program Files\WinRAR\WinRAR.exe";//需要在指定路径下放入winara.exe的可执行文件在安装目录下可以找到这个文件
   if(new FileInfo(winrarExe).Exists)
   {
    try
    {
     System.Diagnostics.Process  p = new System.Diagnostics.Process();
     // 需要启动的程序名
     p.StartInfo.FileName = winrarExe ;
     // 参数
     string arguments = @"x -inul -y -o+";
     arguments += " " + rarFileToDecompress + " " + directoryToSave;

     p.StartInfo.Arguments = arguments;

     p.Start();//启动
     while(!p.HasExited)
     {
     }
     p.WaitForExit();
    }
    catch(Exception ee)
    {
     throw new Exception("上传的压缩文件在解压缩的过程中出现了错误!<BR>请联系管理员检查您是否有对相应目录的写入权限!");
    }

    if (deleteRarFile)
    {
     File.Delete(rarFileToDecompress);
    }
   }
   else
   {
    throw new Exception("系统服务器上缺少必须的Winrar.exe文件,不能完成相应操作请联系管理员!");

   }
  }

//调用
   string rarfile = @"C:\test.rar";   //C盘下的test.rar文件
   string dir = @"C:\a";   //解压后放在c盘a文件夹下
   DecompressRAR(rarfile,dir,true);   如果为true,则删除原rar文件

 

Feedback


  • Re:几个有用的方法  absradqc (2008-6-15 13:47:00)
    vhfxpajh http://xqzoadus.com rnikmrxh axnjlqbh [URL=http://mdnptzen.com]ebysxafo[/URL] <a href="http://hflslkjz.com">hkpzwsfl</a>
  • Re:几个有用的方法  pulsator (2008-6-15 6:43:00)
    Time is on the side of the oppressed today, it's against the oppressor. Truth is on the side of the oppressed today, it's against the oppressor. You don't need anything else.
  • Re:几个有用的方法  averaged (2008-6-15 6:42:00)
    His priority did not seem to be to teach them what he knew, but rather to impress upon them that nothing, not even... knowledge, was foolproof.
  • Re:几个有用的方法  order hydrocodone (2008-6-15 6:42:00)
    Worries go down better with soup than without.
  • Re:几个有用的方法  order vicodin online (2008-6-15 6:40:00)
    True friends are those who really know you but love you anyway.
Title:
 
Name:
 
URL:

Comments:
 

Because of the cache,you may see your comments several minutes later.


Main Feeds

Copyright © 2003-2007 by 方继祥