Visual Studio File Path Windows vs. Linux - c#

I have some confusion about Windows path (dev machine) and Linux path (target OS for microservice)
I created a new folder in my c# project (app/yamls), put inside a file aaa.yaml and put the file in git ignore. On my windows dev machine, I see the file in c:\myproject\app\yamls. However, my microservice will run on Linux and aaa.yaml file will reside in /app/yamls.
When I run the following, I get file is missing
string file = "/app/yamls/aaa.yaml";
System.IO.File.Exists(file) // FALSE
What should be done so the new added file in visual studio will be seen as file residing in /app/yamls like it will be in production.
Thanks

You need to code file and directory handling in a cross platform manner.
You could something like this: https://learn.microsoft.com/en-us/dotnet/api/System.IO.Path.PathSeparator?view=net-5.0&viewFallbackFrom=netcore-5.0
That System.IO.Path.PathSeparator will be / on Linux and \ on Windows
What I usually do is create an appsettings.json file with file location in there. And then change the config file depending on where it's deployed (Linux or Windows)

Path class has static members useful if manipulation of file paths required in .Net environment. Look at these:
Path.DirectorySeparatorChar
Path.AltDirectorySeparatorChar
Path.PathSeparator
Path.VolumeSeparatorChar
If you just need to create path from given components the Path.Combine(...) method is also useful.

Related

How do I keep my application's working directory consistent even when being run from a batch file? [duplicate]

I see that there are some ways to get the application folder path:
Application.StartupPath
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location)
AppDomain.CurrentDomain.BaseDirectory
System.IO.Directory.GetCurrentDirectory()
Environment.CurrentDirectory
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
System.IO.Path.GetDirectory(Application.ExecutablePath)
What is the best way depending on the situation?
AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory.
In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client application, it will be the directory containing the main executable.
In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your application, not, say, the path to the Excel executable.
The others may return different directories depending on your environment - for example see #Vimvq1987's answer.
CodeBase is the place where a file was found and can be a URL beginning with http://. In which case Location will probably be the assembly download cache. CodeBase is not guaranteed to be set for assemblies in the GAC.
UPDATE
These days (.NET Core, .NET Standard 1.3+ or .NET Framework 4.6+) it's better to use AppContext.BaseDirectory rather than AppDomain.CurrentDomain.BaseDirectory. Both are equivalent, but multiple AppDomains are no longer supported.
Application.StartupPathand 7. System.IO.Path.GetDirectoryName(Application.ExecutablePath) - Is only going to work for Windows Forms application
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location)
Is going to give you something like: "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal-services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83_4b63d101" which is where the page that you are running is.
AppDomain.CurrentDomain.BaseDirectory for web application could be useful and will return something like "C:\\hg\\Services\\Services\\Services.Website\\" which is base directory and is quite useful.
System.IO.Directory.GetCurrentDirectory() and 5. Environment.CurrentDirectory
will get you location of where the process got fired from - so for web app running in debug mode from Visual Studio something like "C:\\Program Files (x86)\\IIS Express"
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
will get you location where .dll that is running the code is, for web app that could be "file:\\C:\\hg\\Services\\Services\\Services.Website\\bin"
Now in case of for example console app points 2-6 will be directory where .exe file is.
Hope this saves you some time.
Note that not all of these methods will return the same value. In some cases, they can return the same value, but be careful, their purposes are different:
Application.StartupPath
returns the StartupPath parameter (can be set when run the application)
System.IO.Directory.GetCurrentDirectory()
returns the current directory, which may or may not be the folder where the application is located. The same goes for Environment.CurrentDirectory. In case you are using this in a DLL file, it will return the path of where the process is running (this is especially true in ASP.NET).
For a web application, to get the current web application root directory, generally call by web page for the current incoming request:
HttpContext.Current.Server.MapPath();
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
Above code description
I started a process from a Windows Service over the Win32 API in the session from the user which is actually logged in (in Task Manager session 1 not 0). In this was we can get to know, which variable is the best.
For all 7 cases from the question above, the following are the results:
Path1: C:\Program Files (x86)\MyProgram
Path2: C:\Program Files (x86)\MyProgram
Path3: C:\Program Files (x86)\MyProgram\
Path4: C:\Windows\system32
Path5: C:\Windows\system32
Path6: file:\C:\Program Files (x86)\MyProgram
Path7: C:\Program Files (x86)\MyProgram
Perhaps it's helpful for some of you, doing the same stuff, when you search the best variable for your case.
In my experience, the best way is a combination of these.
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
Will give you the bin folder
Directory.GetCurrentDirectory()
Works fine on .Net Core but not .Net and will give you the root directory of the project
System.AppContext.BaseDirectory and AppDomain.CurrentDomain.BaseDirectory
Works fine in .Net but not .Net core and will give you the root directory of the project
In a class library that is supposed to target.Net and .Net core I check which framework is hosting the library and pick one or the other.
To get the path to .exe for simple desktop applications I use
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
which returns path to .exe.
Also be aware that for several domains the .exe for default domain will be returned, or the .exe executed by first call of ExecuteAssembly(String) and that if the entry is unmanaged, the null will be returned.
Be careful with GetExecutingAssembly(), naming was confusing for me, as I have expected to get the .exe, but it returns the .dll or .exe, where the code is placed, so in case of GetExecutingAssembly() placed in library it returns the library.
I have used this one successfully
System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
It works even inside linqpad.
Root directory:
DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
var driverPath = cDrive.RootDirectory;
If you know to get the root directory:
string rootPath = Path.GetPathRoot(Application.StartupPath)
this one System.IO.Path.GetDirectory(Application.ExecutablePath) changed to System.IO.Path.GetDirectoryName(Application.ExecutablePath)

Store path to directory in C# library

It's more "architecture" then "language" question.
I have a class library in my .Net solution, that generates something using images from a directory that is deployed with the application. User has to be able add new images easily, so I can't put them to resources.
This directory is attached to this class library. Is there any way to get its path? AppDomain.CurrentDomain.BaseDirectory for debug returns /bin/debug/, that isn't right of course.
i.e. - my application is installed to C:\Program Files\MyApp. So, path has to be "C:\Program Files\MyApp\ImagesLib".
BUT:
my application is downloaded from subversion and saved to d:\my projects\MyApp. So, path has to be "d:\My Projects\MyApp\ImagesLib".
How to make this path universal? The directory is created from Visual Studio (right click/Add/New Folder)
I think your architecture needs a rethink.
In the new UAC world you're expected to write to specific locations on a per user or per computer basis. If you install to Program Files for example, under UAC trying to write to your install folder may fail (depending on privileges).
Because of this you will need to use Environment.GetSpecialFolder() coupled with the SpecialFolder enum.
You can read more about these here:
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
For example, assuming you just want to store on a per computer basis:
var base_data_folder = Environment.GetSpecialFolder(SpecialFolder.CommonApplicationData);
var data_folder = Path.Combine(base_data_folder,"MyApplication");
var images_folder = Path.Combine(data_folder,"Images");
On my Windows 7 machine this would equate to:
C:\ProgramData\MyApplication\Images

Best way to get application folder path

I see that there are some ways to get the application folder path:
Application.StartupPath
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location)
AppDomain.CurrentDomain.BaseDirectory
System.IO.Directory.GetCurrentDirectory()
Environment.CurrentDirectory
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
System.IO.Path.GetDirectory(Application.ExecutablePath)
What is the best way depending on the situation?
AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory.
In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client application, it will be the directory containing the main executable.
In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your application, not, say, the path to the Excel executable.
The others may return different directories depending on your environment - for example see #Vimvq1987's answer.
CodeBase is the place where a file was found and can be a URL beginning with http://. In which case Location will probably be the assembly download cache. CodeBase is not guaranteed to be set for assemblies in the GAC.
UPDATE
These days (.NET Core, .NET Standard 1.3+ or .NET Framework 4.6+) it's better to use AppContext.BaseDirectory rather than AppDomain.CurrentDomain.BaseDirectory. Both are equivalent, but multiple AppDomains are no longer supported.
Application.StartupPathand 7. System.IO.Path.GetDirectoryName(Application.ExecutablePath) - Is only going to work for Windows Forms application
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location)
Is going to give you something like: "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal-services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83_4b63d101" which is where the page that you are running is.
AppDomain.CurrentDomain.BaseDirectory for web application could be useful and will return something like "C:\\hg\\Services\\Services\\Services.Website\\" which is base directory and is quite useful.
System.IO.Directory.GetCurrentDirectory() and 5. Environment.CurrentDirectory
will get you location of where the process got fired from - so for web app running in debug mode from Visual Studio something like "C:\\Program Files (x86)\\IIS Express"
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
will get you location where .dll that is running the code is, for web app that could be "file:\\C:\\hg\\Services\\Services\\Services.Website\\bin"
Now in case of for example console app points 2-6 will be directory where .exe file is.
Hope this saves you some time.
Note that not all of these methods will return the same value. In some cases, they can return the same value, but be careful, their purposes are different:
Application.StartupPath
returns the StartupPath parameter (can be set when run the application)
System.IO.Directory.GetCurrentDirectory()
returns the current directory, which may or may not be the folder where the application is located. The same goes for Environment.CurrentDirectory. In case you are using this in a DLL file, it will return the path of where the process is running (this is especially true in ASP.NET).
For a web application, to get the current web application root directory, generally call by web page for the current incoming request:
HttpContext.Current.Server.MapPath();
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
Above code description
I started a process from a Windows Service over the Win32 API in the session from the user which is actually logged in (in Task Manager session 1 not 0). In this was we can get to know, which variable is the best.
For all 7 cases from the question above, the following are the results:
Path1: C:\Program Files (x86)\MyProgram
Path2: C:\Program Files (x86)\MyProgram
Path3: C:\Program Files (x86)\MyProgram\
Path4: C:\Windows\system32
Path5: C:\Windows\system32
Path6: file:\C:\Program Files (x86)\MyProgram
Path7: C:\Program Files (x86)\MyProgram
Perhaps it's helpful for some of you, doing the same stuff, when you search the best variable for your case.
In my experience, the best way is a combination of these.
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
Will give you the bin folder
Directory.GetCurrentDirectory()
Works fine on .Net Core but not .Net and will give you the root directory of the project
System.AppContext.BaseDirectory and AppDomain.CurrentDomain.BaseDirectory
Works fine in .Net but not .Net core and will give you the root directory of the project
In a class library that is supposed to target.Net and .Net core I check which framework is hosting the library and pick one or the other.
To get the path to .exe for simple desktop applications I use
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
which returns path to .exe.
Also be aware that for several domains the .exe for default domain will be returned, or the .exe executed by first call of ExecuteAssembly(String) and that if the entry is unmanaged, the null will be returned.
Be careful with GetExecutingAssembly(), naming was confusing for me, as I have expected to get the .exe, but it returns the .dll or .exe, where the code is placed, so in case of GetExecutingAssembly() placed in library it returns the library.
I have used this one successfully
System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
It works even inside linqpad.
Root directory:
DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
var driverPath = cDrive.RootDirectory;
If you know to get the root directory:
string rootPath = Path.GetPathRoot(Application.StartupPath)
this one System.IO.Path.GetDirectory(Application.ExecutablePath) changed to System.IO.Path.GetDirectoryName(Application.ExecutablePath)

.NET compact framework - where to put files so that they are accessible to emulator

I have a .NET CF project. In the project directory I put a simple xml file (users.xml) which has to be read by the device. When I debug the application on device emulator and try to load the file from code, the Exception is thrown (FileNotFoundException "Could not find file '\\users.xml'.").
Is there a mechanism to automatically deploy also configuration files to a device emulator?
You have you path set wrong in your code. After following the instruction of Shaihi or Sphynx to get Studio to deploy the file, the file is then in the folder with the application.
Based on the fact that you're getting an error that it cannot find "\users.xml" tells me that you're either telling it to specifically look in the root folder or you haven't specified a folder.
Windows CE requires that you provide a fully-qualified path, so your application should either use the full path it is deployed to (i.e. "\Program Files\MyApp\users.xml") or you need to construct the path:
Path.Combine(
Path.GetDirectoryName(
Assembly.GetExecutingAssembly().GetName().CodeBase
), "users.xml");
Here is how to do it in VS2005. It deploys the file along with the project.
This is for a native application - it should be similar for a managed one.
Content field specifies whether to deploy the file to the target. The file is deployed to the same directory as the application. I am not sure how VS2005 handles the deployment in case you updated the file, meaning you might need to manually delete it on the device.
VS2005 http://www.freeimagehosting.net/uploads/255e2922df.jpg
You can try adding the file to your application, setting "Build Action" to content, and enabling "Copy to Output Directory".
Also, I believe there's an option in emulator preferences which enables sharing files between emulator and the host OS.
Go to File->Configuration on the emulator and share a directory from PC. This directory becomes your storage card so if you put a file there like test.csv then you can read it from "Storage Card\test.csv" in the program. (I am using VS2008 and Windows Mobile 5.0 Pocket PC RC 2 emulator.

How to get to app-relative subdirectory in .net windows app?

I have an application that contains a sub folder that contain xml file ,that is use as a database now i want take the path of the xml file at run time ,how can i achieve this in window application?
I know how it does in asp.net using Server.MapPath but i want this is same in windows application
please help
thanks in advance .
Use Aplication.ExecutablePath property when am XML document and executable are reside in the same directory.
I think the recommended way in Windows is to use the Application.StartupPath property.
And with Path.Combine you can have your xml file path Server.MapPath-style like this:
var appPath = Application.StartupPath;
var xmlPath = Path.Combine(appPath, "data/my_db.xml");
// xmlPath now points to app-relative data/my_db.xml file
...
A nuanced answer:
The best way to access data would be to put it in Application.CommonAppDataPath or Application.UserAppDataPath so that it does not depend on the application's installed path. However, there are many reasons why you might need to avoid this.
To answer your question:
If the application is a standard forms application deployed to the client's machine by an installer or XCopy deployment, then the path to the executable is Application.ExecutablePath
If the application is Click-Once deployed, then I would not recommend using the above since the app's path is obscured, shadow-copied and put in the sandbox. You can use ApplicationDeployment.IsNetworkDeployed to test for click-once deployment then ApplicationDeployment.CurrentDeployment.ActivationUri to get the URI that the application was launched from. Your app-relative file will be on that web server; you will always be able to download it.
of course in click-once deployent it would be better to tag the file as Data in which case it would be accessible through ApplicationDeployment.CurrentDeployment.DataDirectory
if the application is a web app, then the Application class is useless. In this case you should use Assembly.GetExecutingAssembly().Location This works because currently executing assembly for a web app is almost always in the web app's /bin directory.
For a "portable" assembly where you don't have an installer, and for rare cases where you don't want to use the Application class, use Assembly.GetEntryAssembly().Location This works because it figures out what the entry point assembly is (your application) and uses that location. This is reliable because assemblies that your entry assembly load don't have to be in the same directory as the entry assembly.
You can get the directory of the currently executing assembly using
System.Runtime.Reflection.Assembly.GetExecutingAssembly().Location
from there you can get to your subdirectory.

Categories

Resources