天天看點

asp.net 檢查檔案夾和檔案是否存在

允許 path 參數指定相對或絕對路徑資訊。 相對路徑資訊被解釋為相對于目前工作目錄。

檢查該目錄是否存在之前,從 path 參數的末尾移除尾随空格。

path 參數不區分大小寫。

如果您沒有該目錄的最小隻讀權限,exists 方法将傳回 false。

if directory.exists(path) then                     ' this path is a directory.                     processdirectory(path)                 else                     console.writeline("{0} is not a valid file or directory.", path)                 end if

下在看一款詳細的執行個體

       //判斷檔案夾的存在、建立、删除檔案夾                         string aaaa = "f:notebookhaha";//路徑的正确寫法             if (directory.exists(aaaa))//如果不存在就建立file檔案夾             {                 messagebox.show("存在檔案夾");                 //directory.delete(aaaa, false);//如果檔案夾中有檔案或目錄,此處會報錯                 //directory.delete(aaaa, true);//true代表删除檔案夾及其裡面的子目錄和檔案             }             else                 messagebox.show("不存在檔案夾");                 directory.createdirectory(aaaa);//建立該檔案夾             //判斷檔案的存在、建立、删除檔案             string dddd = aaaa + "11.txt";             if (file.exists(dddd))                 messagebox.show("存在檔案");                 file.delete(dddd);//删除該檔案                 messagebox.show("不存在檔案");                 file.create(dddd);//建立該檔案,如果路徑檔案夾不存在,則報錯。 <span style="font-family: verdana, 'courier new'" face="verdana, 'courier new'"><span style="font-size: 15px; line-height: 18px; white-space: normal"> </span></span>

關于朋友說file.exists() 對網絡映射盤上的檔案,不論存在與否,一律傳回false下面來看看詳細說明

file.exists()本身沒有問題。您可以試一下,在winform中完成同樣的功能不會出任何錯誤。

式版有比較嚴格的限制。當aspnet權限無權檢視該共享檔案,則傳回false。

有一個workaround:您可以更改該asp.net程式的使用者權限,在web.config檔案中為該asp.net應用程式指定一個

特定的使用者:

<identity impersonate="true" username="accountname" password="password" />

經過測試,這時應該能夠直接檢測到其他機器的共享檔案是否存在:

(檢測網絡映射盤上的檔案仍會有問題)。

繼續閱讀