.NET 中的默认/相对路径解析如何工作?

发布于 2024-07-08 02:49:26 字数 679 浏览 8 评论 0原文

所以...我曾经认为,当您访问一个文件但指定了没有路径的名称(在我的例子中为 CAISLog.csv)时,.NET 会期望该文件与正在运行的 .exe 驻留在同一路径中。

当我单步执行解决方案 (C# .NET2.* VS2K5) 但当我在正常模式下运行应用程序(由 Websphere MQ 触发器监视器启动并作为网络服务在后台运行)而不是访问文件位于 .exe 所在路径,正在 C:\WINDOWS\system32 中查找。 如果重要的话父任务的 .exe 与我的应用程序几乎位于相同的文件夹结构/路径中,

我收到匹配的错误:“System.UnauthorizedAccessException:访问路径 'C:\WINDOWS\system32\CAISLog.csv'被拒绝。

我的解决方法是完全限定我的文件的位置。 然而,我想了解的是“当在 IO 期间仅指定文件名时,控制如何解析路径的 .NET 规则是什么?” 我觉得我缺少一些基本概念,并且这让我很烦恼。

编辑 - 我不确定它本身是一个 .NET 规则,但 Schmuli 似乎更清楚地解释了这个概念。 我将来肯定会尝试 Rob Prouse 的建议,所以也对此+1。

如果有人有一些重新措辞的建议,强调我并不真正关心找到 .exe 的路径 - 而只是不明白相对路径解析发生了什么(而且我可能我的术语仍然搞砸了)...

So... I used to think that when you accessed a file but specified the name without a path (CAISLog.csv in my case) that .NET would expect the file to reside at the same path as the running .exe.

This works when I'm stepping through a solution (C# .NET2.* VS2K5) but when I run the app in normal mode (Started by a Websphere MQ Trigger monitor & running in the background as a network service) instead of accessing the file at the path where the .exe is it's being looked for at C:\WINDOWS\system32. If it matters The parent task's .exe is in almost the same folder structure/path as my app

I get a matching error: "System.UnauthorizedAccessException: Access to the path 'C:\WINDOWS\system32\CAISLog.csv' is denied."

My workaround is to just fully qualify the location of my file. What I want to understand, however is "What is the .NET rule that governs how a path is resolved when only the file name is specified during IO?" I feel I'm missing some basic concept and it's bugging me bad.

edit - I'm not sure it's a.NET rule per se but Schmuli seems to be explaining the concept a little clearer. I will definitely try Rob Prouse's suggestions in the future so +1 on that too.

If anyone has some re-wording suggestions that emphasize I don't really care about finding the path to my .exe - rather just didn't understand what was going on with relative path resolution (and I may still have my terminlogy screwed up)...

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

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

发布评论

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

评论(4

寂寞花火° 2024-07-15 02:49:26

当应用程序 (WinForms) 启动时,Environment.CurrentDirectory 包含应用程序文件夹的路径(即包含 .exe 程序集的文件夹)。 使用任何文件对话框,例如。 OpenFileDialogSaveFileDialog等将导致当前目录发生更改(如果选择了不同的文件夹)。

当运行Windows服务时,它的包含文件夹是C:\Windows\System32,因为这是系统文件夹,并且它是实际运行Windows服务的系统(即操作系统)。

请注意,在大多数 System.IO 对象中指定相对路径,将回退到使用 Environment.CurrentDirectory 属性。

如前所述,有多种方法可以获取服务可执行文件的路径,使用 Assembly.GetEntryAssembly()Assembly.GetExecutingAssembly(),然后使用 Location 属性或 CodeBase 属性(请注意,这是可执行文件的文件路径,而不是目录)。

另一种选择是使用:

`System.IO.Directory.SetCurrentDirectory( System.AppDomain.CurrentDomain.BaseDirectory );`

在服务的 OnStart 方法中进行调用,将其应用于整个应用程序。

When an application (WinForms) starts up, the Environment.CurrentDirectory contains the path to the application folder (i.e. the folder that contains the .exe assembly). Using any of the File Dialogs, ex. OpenFileDialog, SaveFileDialog, etc. will cause the current directory to change (if a different folder was selected).

When running a Windows Service, its containing folder is C:\Windows\System32, as that is the System folder and it is the System (i.e. the Operation System) that is actually running your Windows Service.

Note that specifying a relative path in most of the System.IO objects, will fall back to using the Environment.CurrentDirectory property.

As mentioned, there are several ways to obtain the path of the service executable, using Assembly.GetEntryAssembly() or Assembly.GetExecutingAssembly() and then using either the Location property or the CodeBase property (be aware that this is the file path, not directory, of the executable).

Another option is to use:

`System.IO.Directory.SetCurrentDirectory( System.AppDomain.CurrentDomain.BaseDirectory );`

Make the call in the Service's OnStart method, applying it to the whole application.

或十年 2024-07-15 02:49:26

它基于当前工作目录,该目录可能与应用程序所在的位置相同,也可能不同,特别是如果从不同的程序或具有不同工作目录的快捷方式启动。

不要对路径进行硬编码,而是获取程序的路径并使用它。 您可以使用类似的方法来执行此操作。

Assembly ass = Assembly.GetEntryAssembly();
string dir = Path.GetDirectoryName(ass.Location);
string filename = Path.Combine( dir, "CAISLog.csv" );

这假设条目程序集是您的文件所在的位置。 如果没有,您可以将组件更改为类似的东西;

Assembly ass = Assembly.GetAssembly( typeof( AClassInYourAssembly ) );

It is based on the current working directory which may or may not be the same as where your application resides, especially if started from a different program or a shortcut with a different working directory.

Rather than hard code the path, get the path to your program and use it. You can do this with something like this

Assembly ass = Assembly.GetEntryAssembly();
string dir = Path.GetDirectoryName(ass.Location);
string filename = Path.Combine( dir, "CAISLog.csv" );

This assumes that the entry assembly is where your file is. If not, you can change up getting the assembly for something like;

Assembly ass = Assembly.GetAssembly( typeof( AClassInYourAssembly ) );
篱下浅笙歌 2024-07-15 02:49:26

相对路径解析永远不会对启动可执行文件的路径起作用。 它始终针对进程的“当前目录”起作用,并且您不能真正期望它始终设置为 .exe 所在的目录。

如果您需要这种行为,请小心地自行找出正确的路径并提供文件操作的完全限定路径。

Relative Path resolution never works against the path of the launching executable. It always works against the process' Current Directory, and you can't really expect that to always be set to the directory the .exe lives in.

If you need that behavior, then take care to find out the right path on your own and provide a fully qualified path to the file operations.

感悟人生的甜 2024-07-15 02:49:26

您可以使用它来指定与 exe @"..\CAISLog.csv" 位于同一路径的路径。 请注意,双点指的是 .exe 所在位置的父目录。

温迪

You can use this to specify a path that resides at the same path of your exe @"..\CAISLog.csv". Please note that the double dots refer to the parent directory of where ever your .exe lies.

RWendi

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