2015-05-22 15 views
3

當我需要一個工具並在MSDN上找到它時,我經常發現自己一直在弄清楚我需要什麼指令才能使用它。例如,我需要File.Exists(...),記錄here,說是在System.IO命名空間,但是當我在我的代碼中使用它,我得到一個編譯錯誤如何使用MSDN文檔和Visual Studio來弄清楚我需要什麼「使用」指令

「System.Web.Mvc.Controller.File(字符串,字符串,字符串)」是 ‘方法’,這是不是在給定的情況下

這是沒有意義的,因爲我的背景下

if (!File.Exists(newFileNameAndPath)) 
    throw new Exception(string.Format("File with name {0} already exists in Assets folder. Not overwritten.", newFileName)); 

是簡單有效的,我一直在使用頂部是。那麼這裏有什麼問題?

我猜我實際上需要using System.IO.something,但我不知道我怎麼能找出something是什麼。

+2

兩個System.IO和System.Web.Mvc.Controller包含一個名爲類文件, –

+1

您可以右鍵單擊您的文件類(或任何其他的類名稱必須在相同條件下),然後去化解和你會看到插入正確的選項使用或添加類的完整路徑 –

回答

3

你得到這個,因爲System.Web.Mvc.Controller有方法FileSystem.IOFile類。

您可以使用完全限定類名來防止這種解決衝突:

if (!System.IO.File.Exists(newFileNameAndPath)) 
3

您需要將您的命名空間來File.Exists

如呼叫:

if (!IO.File.Exists(newFileNameAndPath)) 
+0

正確的答案。這只是一個不明確的方法,存在於多個命名空間中。 – ElmoDev001

2

你可以通過使用using alias directive明確說明您想使用的課程類型:

using File = System.IO.File; 

if (!File.Exists(newFileNameAndPath)) 
1

問題是有兩個項目名稱爲File

所以,如果你有兩個using語句,你會得到一個衝突。解決方案是完全符合其中一個(例如:System.IO.File.Exists(newFileNameAndPath))或使用namespace alias

using IO = System.IO; //at the top of the file 

... 

if (!IO.File.Exists(newFileNameAndPath)) 
相關問題