2014-02-09 51 views
0

我知道有很多类似于此的主题,但在查看几十个结果后,我一直无法找到解决方案。Visual Studio 2012 - 访问不同目录中的文件

我有一个项目“富”,我的控制器是在“Foo \ Controllers \ Bar.cs,并在该C#文件中,我想从文件中读取,位于”Foo \ Data \ Stuff.txt “这很简单,但我没有尝试过,主要是因为像Directory.GetCurrentDirectory()和所有类似的内置函数引用执行目录(在我的情况下,”C:\ Program Files(x86)\ IIS Express“)。

我是什么做错了吗?或者,如果我错过了一个相同的问题,请大家告诉我有,这似乎小的问题已经花了这么多时间,谢谢!

+1

尝试使用['MapPath'](http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v = vs .110).aspx) – dkozl

+0

这绝对是最简单的,谢谢! – Befall

回答

1

随着命令使用Server.Mappath(“富\ DATA \ Stuff.txt” )你会发现存储文件的物理路径

+0

使用dkozl的链接,但它是工作的MapPath,谢谢! – Befall

0

这听起来像你可能会寻找为System.Reflection.Assembly.GetExecutingAssembly().CodeBase,它可以让你找到exac tly你运行的.exe所在的位置;不管你是否在调试器中。

下面是一个使用“基本代码”寻找路径的例子,然后读取从.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(); 
}