簡體   English   中英

Visual Studio 2012-訪問其他目錄中的文件

[英]Visual Studio 2012 - access file in different directory

我知道有很多與此主題類似的內容,但是在查看了幾十個結果之后,我一直無法找到解決方案。

我有一個項目“ Foo”,我的控制器位於“ Foo \\ Controllers \\ Bar.cs”,並且在該C#文件中,我想從位於“ Foo \\ Data \\ Stuff.txt”的文件中讀取數據。簡單,但我嘗試過的任何方法都沒有用,主要是因為Directory.GetCurrentDirectory()類的東西和所有類似的內置函數都引用執行目錄(在我的情況下為“ C:\\ Program Files(x86)\\ IIS Express”)。

我究竟做錯了什么? 或者,如果我錯過了一個相同的問題,請指示我到那里,因為花費了這么多時間,這似乎是一個小問題。 謝謝!

使用命令Server.MapPath(“ Foo \\ Data \\ Stuff.txt”),您將找到存儲文件的物理路徑

聽起來您可能正在尋找System.Reflection.Assembly.GetExecutingAssembly().CodeBase ,它使您可以精確找到正在運行的.exe的位置; 無論您是否在調試器中。

這是一個使用“ CodeBase”查找路徑,然后從.exe讀取Windows版本信息的示例:

// GetWindowsVersion: Fetch Winver info from specified file
public static string GetWindowsFileVersion()
{
    String codeBaseUri =
        System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    String codeBase =
        new Uri(codeBaseUri).LocalPath;
    System.Diagnostics.FileVersionInfo info = FileVersionInfo.GetVersionInfo(codeBase);
    return info.FileVersion.ToString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM