2013-06-07 2 views
0

이 내 코드 분류에 따르면File.exists (Filename) 문이 작동하지 않는 이유는 무엇입니까?

protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    if (FileUploadControl.HasFile) 
    { 
     try 
     { 
      string filename = Path.GetFileName(FileUploadControl.FileName); 
      switch(SubDrpDownList.SelectedIndex) 
      { 
       case 0: 
        if (!File.Exists(filename)) 
         FileUploadControl.SaveAs(Server.MapPath("~/Books/Math/") + filename); 
        else if (File.Exists(filename)) 
        { 
         throw new DuplicateWaitObjectException(); 
        } 
        break; 
       case 1: 
        if (!File.Exists(filename)) 
        FileUploadControl.SaveAs(Server.MapPath("~/Books/Physics/") + filename); 
        else if (File.Exists(filename)) 
        { 
         throw new DuplicateWaitObjectException(); 
        } 
        break; 
       case 2: 
        if (!File.Exists(filename)) 
        FileUploadControl.SaveAs(Server.MapPath("~/Books/Drawing/") + filename); 
        else if (File.Exists(filename)) 
        { 
         throw new DuplicateWaitObjectException(); 
        } 
        break; 
      } 
      lblStatus.Text = "Upload status: File uploaded!"; 
     } 
     catch (Exception ex) 
     { 
      lblStatus.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
     } 
    } 
} 

, 나는 책을 내가 스위치 문을 사용하여 달성 diffrenet 폴더에 업로드 할 수 싶습니다. 문제는 내가 책을 업로드 한 다음 다시 업로드 할 때 File.Exists (filename)가 작동하지 않는다는 것입니다. 내가 원하는 것은 같은 파일을 두 번 업로드하면 중복 파일이 업로드 되려고한다는 예외가 발생합니다. 단, case 문장의 경우 File.Exists (filename) 라인 만 중복 된 파일이라도 실행됩니다. File.Exists (filename)이 실행되지 않는 이유는 무엇입니까 ??

답변

1

문제는 파일의 전체 경로가 아닌 파일 이름만을 기준으로 파일이 존재하는지 확인하는 것입니다. 무엇을해야하는 것은이 파일을 업로드하는 경우 것이 있기 때문에, 파일을 확인 어디에 존재

또한, ELSEIF 그냥 다른 있어야 다른 사실을 설명합니다

protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    if (FileUploadControl.HasFile) 
    { 
     try 
     { 
      string filename = Path.GetFileName(FileUploadControl.FileName); 
      switch(SubDrpDownList.SelectedIndex) 
      { 
       case 0: 
        if (!File.Exists(Server.MapPath("~/Books/Math/") + filename)) 
         FileUploadControl.SaveAs(Server.MapPath("~/Books/Math/") + filename); 
        else 
        { 
         throw new DuplicateWaitObjectException(); 
        } 
        break; 
       case 1: 
        if (!File.Exists(Server.MapPath("~/Books/Physics/") + filename)) 
        FileUploadControl.SaveAs(Server.MapPath("~/Books/Physics/") + filename); 
        else 
        { 
         throw new DuplicateWaitObjectException(); 
        } 
        break; 
       case 2: 
        if (!File.Exists(Server.MapPath("~/Books/Drawing/") + filename)) 
        FileUploadControl.SaveAs(Server.MapPath("~/Books/Drawing/") + filename); 
        else 
        { 
         throw new DuplicateWaitObjectException(); 
        } 
        break; 
      } 
      lblStatus.Text = "Upload status: File uploaded!"; 
     } 
     catch (Exception ex) 
     { 
      lblStatus.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
     } 
    } 
} 

입니다 그것은 존재하거나 그것이 "else"에 의해 함축되어 있다는 것을 다시 확인할 필요가 없습니다.

1

당신이 가진 문제는 전체 파일 이름을 줄이는 것입니다. 경로)을 여기에 파일 이름으로 바꿉니다 :

string filename = Path.GetFileName(FileUploadControl.FileName); 

그 시점부터! File.Exists는 항상 true입니다.

예 : "C : \ Somepath \ myfile.txt"를 "myfile.txt"로 줄이고 그런 다음 있는지 확인하십시오.

관련 문제