在调试模式下运行的程序中的相对路径[VS2010]

发布于 2024-10-01 07:41:52 字数 326 浏览 0 评论 0原文

我现在正在 Visual C# 中调试程序,在程序中我使用相对路径访问文件。

  Stream s = File.Open("usr.dat", FileMode.OpenOrCreate);

当前将其吐出在(项目文件夹)/bin/x86/Debug 文件夹(已编译的可执行文件所在的位置)中。有没有办法改变相对路径打开的位置?

(我想这样做的原因是因为我有多个项目,但我希望所有项目都可以访问文件 usr.dat,所以如果您有其他解决方案,我会很乐意接受) 。

编辑:我发现我可以简单地更改属性中的项目工作目录。谢谢你们的意见,伙计们

I'm debugging a program right now in visual C#, and in the program I access a file using a relative path.

  Stream s = File.Open("usr.dat", FileMode.OpenOrCreate);

This is currently spitting it out in the (Project Folder)/bin/x86/Debug folder(where the compiled executable is). Is there a way to change where the relative path opens out to?

(The reason I'd want to do this is because I have multiple projects yet I'd want to have the file usr.dat be accessible to all of them, so if you have another solution to this I'll gladly take it).

EDIT: I found that I can simply change the projects working directory in the properties. Thanks for the input, guys

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

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

发布评论

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

评论(3

晚风撩人 2024-10-08 07:41:52

是的,你不能就这样离开它。当您在启用了 UAC 或使用非管理员用户帐户运行的计算机上部署程序时,这将失败。您对存储 EXE 的目录没有写权限。

查看Environment.GetFolderPath() 以查找您将具有写入访问权限的appdata 文件夹之一。

Yes, you cannot leave it like this. This will fail when you deploy your program on a machine that has UAC enabled or runs with a non-admin user account. You won't have write privileges to the directory in which your EXE is stored.

Review Environment.GetFolderPath() to find one of the appdata folders that you'll have write access to.

歌枕肩 2024-10-08 07:41:52

不要使用“当前目录” - 这是 1980 年代的方法,很容易被破坏(例如,如果您使用文件打开对话框,用户可以将当前目录保留在任何地方,用户可以在启动应用程序时更改当前目录, ETC。)。

因此,要找出从哪里开始,您可以使用Application.StartupPath。

在 Vista/Win7 中,您不能/不应该在程序文件中的任何位置写入 - 因此,如果它是只读文件,则仅使用相对于您的 .exe 的路径。在这种情况下,您可以将程序安装到“Program Files\MyCompany\ApplicationName”并将共享文件安装到“Program Files\MyCompany\Shared”,然后使用 Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath) ))、“Shared”) 作为从中读取共享文件的文件夹。

如果您希望写入该文件,则应使用用户的“我的文档”文件夹(用于用户可见的数据)或“应用程序数据”文件夹(用于您希望对用户“隐藏”的内部数据)。使用Environment.GetFolderPath()来访问这些路径(例如Environment.SpecialFolder.ApplicationData等)

Don't use the "current directory" - this is a 1980's approach and it's easily broken (e.g. if you use a file open dialog, the user can leave the current directory anywhere, the user can change the current directory when lanching your application, etc.).

So, to find out where to start from, you could use Application.StartupPath.

In Vista/Win7, you can't/shouldn't write anywhere in Program Files - therefore, only use a path relative to your .exe if it is to a read-only file. In this case, you could install your programs into "Program Files\MyCompany\ApplicationName" and install the shared files into "Program Files\MyCompany\Shared", and use Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath)), "Shared") as the folder to read shared files from.

If you wish to write to the file, then you should use the user's My Documents folder (for data visible to the user), or the Application Data folder (for internal data you wish to "hide" from the user). Use Environment.GetFolderPath() to access these paths (e.g. Environment.SpecialFolder.ApplicationData, etc)

烏雲後面有陽光 2024-10-08 07:41:52

EXE 的当前位置被用作您正在处理的文件的默认路径,因为您只指定了文件名。如果您在文件名之前使用文件路径(包含完整文件夹层次结构的字符串),则应该可以解决问题。

您可能希望将路径保存为单独的字符串变量并将其与文件名连接起来。将路径分隔为变量还可以帮助您在 File 函数中验证它,例如 File.Exists("path\to\your\file\file.ext")

The EXE's current location is being used as the default path for the file you are working on because you only specified the file name. If you use a file path (a string containing the complete folder hierarchy) before the file name, it should do the trick.

You might want to save the path as a separate string variable and concatenate it with the file name. Separating the path into a variable would also help you verify it in File functions like File.Exists("path\to\your\file\file.ext")

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