在ASP.NET里怎么判断一个文件是否存在?用网络路径,C#

解决方案 »

  1.   

    文件路径可以使用相对路径和决定路径,例如:
    File.Exists(Server.MapPath("../xx.txt"))
      

  2.   

    DirectoryInfo dirinfo=new DirectoryInfo(Server.MapPath("backupdata"));
    if(!dirinfo.Exists)
    {
    System.IO.Directory.CreateDirectory(Server.MapPath("backupdata"));
    }
    else
    {
    if(System.IO.File.Exists(Server.MapPath("backupdata/data.dat")))
    {
    string strcnn=System.Configuration.ConfigurationSettings.AppSettings["strcnn"];
    SqlConnection cnn=new SqlConnection(strcnn);
    SqlCommand cmd=new SqlCommand("admindata1",cnn);
    cmd.CommandType=CommandType.StoredProcedure;
    try
    {
    cnn.Open();
    cmd.ExecuteNonQuery();
    cmd.Dispose();
    }
    catch(Exception E)
    {
    Response.Write(E.Message);
    }
    finally
    {
    cnn.Close();
    cnn.Dispose();
    }
      

  3.   

    File只能对本地的文件进行操作,而我要用的文件为网络文件,如:http://www.google.com/images/logo_sm.gif其实也就是在程序里读取一下指定的网络文件,如果存在就返回一个值,不存在就返回不存在的值,但我不知道怎么在在。NET里读取网络文件。
      

  4.   

    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemNetWebClientClassOpenReadTopic.htmwebclient
    看看这个类[C#] 
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    // Download home page data. 
    Console.WriteLine("Accessing {0} ...",  uriString);                        
    // Open a stream to point to the data stream coming from the Web resource.
    Stream myStream = myWebClient.OpenRead(uriString);Console.WriteLine("\nDisplaying Data :\n");
    StreamReader sr = new StreamReader(myStream);
    Console.WriteLine(sr.ReadToEnd());
    // Close the stream. 
    myStream.Close();
      

  5.   

    http://www.google.com/intl/zh-CN_ALL/images/logo.gif
      

  6.   

    我现在用WebClient这个类解决了,但我在捕获WebClient.openreader的错误时怎么才能只捕获一个错误号呢?像404 ,200这样的.