asp.net如何判断服务器上的目录或文件是否存在

    • asp.net判断服务器上的目录或文件是否存在!(实例)

       

      // ======================================================= 【判断文件是否存在】

       

      [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
       
      1. using System.IO;  // 还需要命名空间,别忘了  
      [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
       
      1. if (System.IO.File.Exists("c:\\aaa.txt"))    // 注意双引号路径应为双斜杠  
      2. {  
      3.     //########## 有aaa.txt文件!  
      4. }  

       

      // ======================================================= 【判断目录是否存在】

       

      [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
       
      1. using System.IO;  // 还需要命名空间,别忘了  
      [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
       
      1. if (System.IO.<span style="font-size: 14px; line-height: 26px; ">Directory</span>.Exists("c:\\aaa"))    // 注意双引号路径应为双斜杠  
      2. {  
      3.     //########## 有aaa文件夹!  
      4.       
      5. }  
       
      // ========================================================= 【创建删除等,网上找的,没测试】
      [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
       
      1. <span style="font-size: 14px; line-height: 26px; ">System.IO.Directory.CreateDirectory(Server.MapPath("file"));   // 创建文件夹 </span>     
      [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
       
      1. <span style="font-size: 14px; line-height: 26px; ">      System.IO.Directory.Delete(Server.MapPath("file"),true);//删除文件夹以及文件夹中的子目录,文件</span>  
      [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
       
      1. <span style="font-size: 14px; line-height: 26px; "><span style="font-size: 14px; line-height: 26px; ">      Directory.Create(Server.MapPath("~/Back/Data.xml"));//创建该文件</span>  
      2. </span>  

       

       

      转载自:http://www.hello-code.com/blog/asp.net/201312/2484.html

posted @ 2014-01-24 16:51  Ziv_zhong  阅读(1323)  评论(0编辑  收藏  举报