2012-08-16 113 views
0

這是文件路徑有效?:有效的文件路徑

'C:sample.txt的'

(這個字符串是從用戶輸入)

這是令人困惑,因爲有沒有錯誤返回。我已經使用Directory.Exists(path.DirectoryName)。 但我找不到在C:中創建的文件。

回答

-2

必須改爲

C:\sample.txt 
1

這的確是一個corret路徑時,Windows犯規了\麻煩。

另請嘗試使用File.Exists()。

0

路徑必須是如下,

 string filePath=args[0] 
     if (!File.Exists(filePath)) 
     { 
      File.Create(filePath); 
     } 
2

是,C:sample.txt是一個有效的文件路徑,並指任何用於驅動C:進程當前目錄(因爲沒有具體的目錄路徑中提供)。

+1

操作系統對每個進程「當前目錄」,併爲每個驅動器盤符創建。所以當你使用'C:sample.txt'時,Windows使用C:的當前目錄作爲你的進程。當前目錄的內容取決於您的流程是如何開始的以及之前已經完成的。 – 2012-08-16 09:09:12

4

它在當前目錄

 string filepath = "c:sample.txt"; 

     StreamWriter sw = File.CreateText(filepath); 
     sw.WriteLine("hello"); 
     sw.Close(); 

     string s = Directory.GetCurrentDirectory(); 
     Console.WriteLine(s);