当前工作目录是visual studio目录 [英] Current working directory is visual studio directory
问题描述
我正在构建一个引用其工作目录中的文件的类库.但是,它无法找到该文件,因为它一直在 C:\Program Files\Visual Studio 2010\Common7\IDE\
中查找.解决方案不在该目录中,工作目录也未设置为该目录.我在那个文件夹中也看不到与我的类库相关的任何内容!
I'm building a class library which references a file in its working directory. However, it's unable to find the file because it keeps looking in C:\Program Files\Visual Studio 2010\Common7\IDE\
. The solutions is not in that directory, nor is the working directory set to that. I can't see anything related to my class library in that folder either!
在构建文件路径时,无论我是否使用 Environment.CurrentDirectory
都会发生这种情况.
This happens whether I use Environment.CurrentDirectory
or not, when constructing my file path.
有人知道它为什么会这样做吗?
推荐答案
从它的声音来看,您希望在构建文件路径时使用 Application.StartupPath
,因此类似于以下内容;
From the sounds of it you want to be using Application.StartupPath
when constructing your file path so something along the lines of;
System.IO.Path.Combine(Application.StartupPath, "myfile.txt");
如果您想将当前目录设置为正确的目录(尽管我个人建议使用 Application.StartupPath
而不是相信当前目录是正确的),您可以这样做;>
If you want to set the current directory to the right directory (although personally I would recommend using the Application.StartupPath
rather than trust the current directory will be right) you can do this;
System.IO.Directory.SetCurrentDirectory(Application.StartupPath);
至于原因,我曾遇到过这样的情况:打开文件打开对话框,然后导航到另一个位置,然后该位置成为当前目录.
As for why, I've had occasions where you do something like open a file open dialog then navigate to another location, which then becomes the current directory.
这篇关于当前工作目录是visual studio目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!