我想刪除一個zip文件在我的代碼C#刪除在C#應用程序的壓縮文件
try
{
System.IO.File.Delete(@"C:/Projets/Prj.zip");
}
catch { }
,但我有這個錯誤The format of the given path is not supported.
爲什麼此異常出現?我該如何解決這個錯誤?
我想刪除一個zip文件在我的代碼C#刪除在C#應用程序的壓縮文件
try
{
System.IO.File.Delete(@"C:/Projets/Prj.zip");
}
catch { }
,但我有這個錯誤The format of the given path is not supported.
爲什麼此異常出現?我該如何解決這個錯誤?
您使用正斜槓,而不是backslahes,導致:
try
{
System.IO.File.Delete(@"C:\Projets\Prj.zip");
}
catch { }
這似乎有些奇怪的字符在某處使它無效已經下滑。如果我複製/粘貼上面的行,它會給我同樣的例外。但是,如果我刪除字符串,並以手工鍵入它,它會給我一個FileNotFound
(顯然)。
嘗試COP /粘貼此行:
System.IO.File.Delete(@"C:\Projets\Prj.zip");
經過進一步調查,罪魁禍首似乎是"
和C
之間的invsible字符。具體來說,存在「從左到右嵌入」的Unicode字符。如果我的字符串轉換爲Unicode,你可以清楚地看到:
System.IO.File.Delete(@"‪C:\Projets\Prj.zip");
沒有用'/'和''\\更新 –
我的回答 –
在Windows文件路徑中使用反斜槓,而不是正斜槓:
System.IO.File.Delete(@"C:\Projets\Prj.zip");
沒有一樣以'/'和\ –
File.Delete還將與slashs工作異常相同的異常 - 必須是另一個原因。 – JeffRSon
嘗試
string file = @"C:\Projets\Prj.zip";
if(System.IO.File.Exists(file))
System.IO.File.Delete(file);
使用Path
庫,以訪問平臺獨立的路徑操作。
var root = "C:" + Path.DirectorySeparatorChar;
var path = Path.Combine(root, "Projects", "Prj.zip");
File.Delete(path); //will try to delete C:\Projects\Prj.zip
現在不錯,但是我的錯誤是什麼? –
您的文件夾分隔符不正確。在Windows中它是'\',但是你有'/' –
看到我的答案奇怪的事情發生。看起來相同的兩條線行爲不一樣。看起來有些古怪的東西溜進了你的生產線。可能是由於您的計算機上使用了不同的charachter集合,請查看您來自哪裏。 –
的更多細節:例如在下面給出? – JeffRSon