string path="文件所在目录的绝对路径";
string[] FilesList=System.IO.Directory.GetFiles(path);
foreach(string filename2 in FilesList)
{
string filename1=filename2;
int j=filename1.LastIndexOf("\\");
filename1=filename1.Substring(j+1);
if(filename1==filename)
{
myLabel.Text="该文件已经存在!<font color=red>|</font>请重新上传<font color=red>|</font>或另存文件名";
return;
}

解决方案 »

  1.   

    bool bExist = System.IO.File.Exists(Server.MapPath("FilePath"));
      

  2.   

    File.Exists(文件名)可以看出文件是否存在!
      

  3.   

    use System.IO.DirectoryInfoto 建立目录, use 
    System.IO.DirectoryInfo di = System.IO.Directory.CreateDirectory("FilePath");tryusing System;
    using System.IO;
    using System.Management;class TestDir
    {
     public static void Main()
     {
    string s = @"d:\labs\html";
    DirectoryInfo di = new DirectoryInfo(s);
    if (di.Exists)
    {
    if ((di.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
    Console.WriteLine(di.FullName + " not readonly");
    } Console.WriteLine(di.Root);
    ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\""+ di.Root.Name.Substring(0,2) +"\"");
            Console.WriteLine("Free space on {0} drive is: {1}", di.Root.Name, disk.Properties["FreeSpace"].Value); }
    }
      

  4.   

    谢谢思归的代码!但是下面这段中有个地方我不太理解:string s = @"d:\labs\html";
    DirectoryInfo di = new DirectoryInfo(s);if ((di.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
    Console.WriteLine(di.FullName + " not readonly");di.Attributes 是不是d:\labs\html所属目录的属性?
    那FileAttributes.ReadOnly又是什么呢?而且,如果d:\labs\html的上上一级(上两级)目录不允许访问,那还是不能通过阿。
      

  5.   

    给个实例:
    ONCLICK事件里进行操作!
    /////////////////////////////////////////////////////////////////
    // 获取上传文件名  
    string strPhotoName = upload.PostedFile.FileName.ToString();     
    // 获取相对地址  
    string strPhotoNameMapPath = Server.MapPath ( "../../Photo/" ); strPhotoName = System.IO.Path.GetFileName( strPhotoName );// 文件的全地址
    string strFullFileName = strPhotoNameMapPath+strCompanyName+"\\"+strPhotoName; 

    // 图片存入数据库的地址
    string strPhotoPath = strCompanyName+"/"+strPhotoName;// 文件的存放文件夹地址
    string strMapPath = strPhotoNameMapPath + strCompanyName;       // 判断上传文件夹是否存在
    if ( System.IO.Directory.Exists( strMapPath ) )                 
    {
      // 判断该图片已经上传
      if( System.IO.File.Exists ( strFullFileName ) )
       {
          Response.Write( "该文件已经存在!" );
       }
       else
       {
          // 进行上传操作
          upload.PostedFile.SaveAs( strFullFileName );
          // 把文件地址放到数据库中
          strSqlUserInfo="execute sp_tbPhoto_AddPhoto @strBlockID='"+strBlockID+"',@strCompanyID='"+strCompanyID+"',@strChild='"+strChild+"',@strCheckLink='"+strCheckLink+"',@strTitle='"+strTitle+"',@strPhotoType='"+strPhotoType+"',@strPhotoPath='"+strPhotoPath+"',@strLinkPath='"+strPhotoPath+"'";
          clsCommon clsCom=new clsCommon();      // 采用WebService进行操作,你的可以用的方式来更新数据库!
          Boolean dsRetu=clsCom.UpdateData(clsCommon.strCondbMemorabilia.ToString(),strSqlUserInfo);
          Response.Redirect( "wfmPhotoAdd.aspx" );
        }
      }
    else
    {
      // 创建文件夹
      System.IO.Directory.CreateDirectory ( strMapPath );
      upload.PostedFile.SaveAs( strFullFileName );
    }