How does the default / relative path resolution work in .NET?

So ... I used to think that when you accessed the file, but specified the name without a path (CAISLog.csv in my case), NET expected the file to be on the same path as the working .exe,

This works when I go through the solution (C # .NET2. * VS2K5), but when I launch the application in normal mode (it starts using the Trigger WebSphere MQ monitor and works in the background as a network service) instead of accessing the file along the way, where it is located in the directory C: \ WINDOWS \ system32. If it matters. Parent .exe task is in almost the same folder structure / path as in my application

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

My workaround is to completely determine the location of my file. However, I want to understand that "What is a .NET rule that defines how a path is resolved when only the file name is specified during I / O?" I feel that I am lacking an elementary concept and it annoys me badly.

edit - I'm not sure if this is a .NET rule as such, but Shmuli seems to explain the concept a bit more clearly. I will definitely try Rob Puze's suggestions in the future, so +1 too.

If someone has some reformulation suggestions that emphasize that I really don't care about finding the path to my .exe - I probably just didn’t understand what was happening with the relative resolution of the path (and I can still have mine terminology screwed) ...

+7
c # file-io visual-studio
source share
4 answers

When the application (WinForms) starts, Environment.CurrentDirectory contains the path to the application folder (i.e. the folder containing the .exe assembly). Using any of the File Dialogs, for example. OpenFileDialog , SaveFileDialog etc. will change the current directory (if another folder is selected).

When you start a Windows service, its containing folder is C: \ Windows \ System32, since it is a system folder, and it is the system (that is, the operating system) that actually starts your Windows service.

Note that specifying a relative path in most System.IO objects will revert to using the Environment.CurrentDirectory property.

As already mentioned, there are several ways to get the path to the service executable using Assembly.GetEntryAssembly() or Assembly.GetExecutingAssembly() , and then using the Location property or CodeBase property (remember that this file is a path, not an executable directory).

Another option is to use:

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

Make a call in the Service OnStart method, applying it to the entire application.

+10
source share

It is based on the current working directory, which may or may not coincide with the location of your application, especially if it is launched from another program or shortcut with a different working directory.

Instead of hard-coded the path, find the path to your program and use it. You can do it 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 record assembly is where your file is located. If not, you can change the assembly for something like:

 Assembly ass = Assembly.GetAssembly( typeof( AClassInYourAssembly ) ); 
+9
source share

Relative path resolution never works against the path of the executable file. It always works against the Current Directory process, and you cannot expect it to always be installed in the directory in which the .exe is located.

If you need this behavior, take care to find the correct path yourself and provide the full path to the files.

+4
source share

You can use this to indicate a path that is on the same path as your exe @ ".. \ CAISLog.csv". Note that double dots refer to the parent directory of your .exe somewhere.

Rwendi

-2
source share

All Articles