石家庄建站平台

十年经验 优惠价格 贴心服务

服务热线 15383239821

asp.net中C#文件和文件夹的复制、删除、移动实现程序

发布时间:2013-10-30    来源:

本文章介绍了几个实例关于asp.net中C#文件和文件夹的复制、删除、移动实现程序有需要的朋友可参考一下。
 

 代码如下
public class SimpleFileCopy{

static void Main()      {

string fileName = "test.txt";

string sourcePath = @"C:UsersPublicTestFolder";

string targetPath = @"C:UsersPublicTestFolderSubDir";

string sourceFile = System.IO.Path.Combine(sourcePath, fileName);

string destFile = System.IO.Path.Combine(targetPath, fileName);

if (!System.IO.Directory.Exists(targetPath))          {             
System.IO.Directory.CreateDirectory(targetPath);          }

 System.IO.File.Copy(sourceFile, destFile, true);

if (System.IO.Directory.Exists(sourcePath)){
string[] files = System.IO.Directory.GetFiles(sourcePath);
 foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);                 
destFile = System.IO.Path.Combine(targetPath, fileName);                
 System.IO.File.Copy(s, destFile, true);              }         
} else {             
Console.WriteLine("Source path does not exist!");         
}

Console.WriteLine("Press any key to exit.");         
Console.ReadKey();      } 
}    
 

下面的示例演示如何移动文件和目录。

 代码如下
public class SimpleFileMove {

static void Main()     
{

string sourceFile = @"C:UsersPublicpublictest.txt";

string destinationFile = @"C:UsersPublicprivatetest.txt";

System.IO.File.Move(sourceFile, destinationFile);

System.IO.Directory.Move(@"C:UsersPublicpublictest", @"C:UsersPublicprivate");    
 }  }    
 

下面的示例演示如何删除文件和目录。

 代码如下

C#
public class SimpleFileDelete  {

static void Main()      {

if(System.IO.File.Exists(@"C:UsersPublicDeleteTesttest.txt"))        
 {

try {                
 System.IO.File.Delete(@"C:UsersPublicDeleteTesttest.txt");             
}

catch (System.IO.IOException e)             
{                 
Console.WriteLine(e.Message); return;            
 }
   }

 

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:UsersPublicDeleteTesttest2.txt");

try {              fi.Delete();         
}
catch (System.IO.IOException e)        
 {            
 Console.WriteLine(e.Message);         
}

 

try {             
System.IO.Directory.Delete(@"C:UsersPublicDeleteTest");        
 } catch (System.IO.IOException e)       
  {              Console.WriteLine(e.Message);        
 }


if(System.IO.Directory.Exists(@"C:UsersPublicDeleteTest"))     
 { try {               
  System.IO.Directory.Delete(@"C:UsersPublicDeleteTest", true);
 } catch (System.IO.IOException e) {  
  Console.WriteLine(e.Message);
 }
 }

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:UsersPublicpublic");

try {
 di.Delete(true);
} catch (System.IO.IOException e)
  {
 Console.WriteLine(e.Message);
 }
 }}
 


还没开展互联网业务? 马上开始

关注多多建站公众号
扫码加好友