当前工作目录是Visual Studio目录

发布于 2024-11-27 15:56:37 字数 266 浏览 1 评论 0原文

我正在构建一个类库,它引用其工作目录中的文件。但是,它无法找到该文件,因为它一直在 C:\Program Files\Visual Studio 2010\Common7\IDE\ 中查找。解决方案不在该目录中,工作目录也没有设置为该目录。我在该文件夹中也看不到与我的类库相关的任何内容!

无论我在构建文件路径时是否使用Environment.CurrentDirectory,都会发生这种情况。

有人知道为什么会这样做吗?

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!

This happens whether I use Environment.CurrentDirectory or not, when constructing my file path.

Does anybody know why it might be doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-12-04 15:56:37

从它的声音来看,您希望在构建文件路径时使用 Application.StartupPath ,因此类似于;

System.IO.Path.Combine(Application.StartupPath, "myfile.txt");

如果您想将当前目录设置为正确的目录(尽管我个人建议使用 Application.StartupPath 而不是相信当前目录是正确的),您可以这样做;

System.IO.Directory.SetCurrentDirectory(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");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文