2013-01-08 55 views
0

我試圖將數據保存到這樣的文件:爲什麼會出現「路徑中的非法字符」錯誤?

FileStream file = new FileStream("c:\temp", FileMode.Create, System.IO.FileAccess.Write); 
     byte[] bytes = new byte[file.Length]; 
     file.Read(bytes, 0, (int)file.Length); 
     file.Write(bytes, 0, bytes.Length); 
     file.Close(); 
     file.Close(); 

而且我得到這個錯誤:

路徑中具有非法字符。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.ArgumentException:路徑中的非法字符。

我在做什麼錯?

回答

0

您需要轉義反斜線在你的文件路徑:

FileStream("c:\\temp", ... 

\t是一個選項卡的字符。

2

嘗試「c:\\ temp」 - 「c:\ temp」是字符串[c] [:] [製表符] [e] [m] [p]這幾乎肯定不是您想要的文件名!

1

使用下列任一

FileStream file = new FileStream(@"c:\temp", FileMode.Create, System.IO.FileAccess.Write); 
FileStream file = new FileStream("c:\\temp", FileMode.Create, System.IO.FileAccess.Write); 

「\」是一個轉義字符,所以你不能直接使用它