In my project I am referring to a folder:
string path=Path.Combine(#"E:\Per\kamlendra.sharma\Windows\main\software\my.software\my.software.Server\Resources", string.Format("LocalizationDictionary.{0}.xaml", SelectedNewLanguage.culture));
But I don't want to hard code this address:
#"E:\Per\kamlendra.sharma\Windows\main\software\my.software\my.software.Server\Resources"
Can anyone please suggest a better approach?
You can store application data in app.config
The UNC path of the currently executing assembly can be obtained. You can then use this as the basis to access the specific subfolder - this is assuming the folder you are looking for is the a subfolder of where the assembly is located...
System.Reflection.Assembly.GetExecutingAssembly().Location //This actually returns the assembly file name, so you would need to use FileInfo to get the folder location.
A better approach is probably System.Appdomain, which gives you access to the location of the actuall WPF application rather than the assembly.
System.AppDomain.CurrentDomain.BaseDirectory
This is how to configure and use application data files.
WPF Application Resource, Content, and Data Files
Related
I've inherited a .Net desktop app that has a folder called Data that's in the same folder as the executable. Currently, the app just opens "Data" but that breaks if the current directory is not the directory that contains the executable. I've found two ways of getting the executable's folder: Assembly.GetExecutingAssembly().Location and AppDomain.CurrentDomain.BaseDirectory. The latter is easier to use since I can just append "Data" to the string. Will AppDomain.CurrentDomain.BaseDirectory always return the executable's directory or are there situations where it might not?
Short answer
Both should be perfectly fine in 99% of cases.
Explanation
The currently executing assembly's directory is a reliable way to get the location of the current executable. Using System.IO, you could simply remove the file name from the path:
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
If the Data folder is shipped as part of a dll file that you compile, you might want to use typeof(SomeClassInMyDll).Assembly.Location instead of the executing assembly. This is to allow for a user of your DLL to have uncommon logic such as loading your assembly based on its path, which might be a different location than the currently executing assembly.
The AppDomain.BaseDirectory property is the location where the currently running code looks for DLL files when it needs to load a dependency. This is generally the same location as the currently executing assembly's path, however, if whoever is calling your code is loading your DLL in a separate AppDomain, again loading it from a file name explicitly, the BaseDirectory might be different from the folder that contains your DLL or executable.
To give an example of that uncommon scenario, let's say there is an executable C:\foo\bar.exe, which dynamically loads a DLL from a different path. This would be done by calling the method Assembly.LoadFrom("C:\MyFolder\MyDll.dll"). In that case, the following would be returned:
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) = C:\foo\
AppDomain.BaseDirectory = C:\foo\
Path.GetDirectoryName(typeof(SomeClassInMyDll).Assembly.Location) = C:\MyFolder\
Maybe this?
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
There isn't any preferred way, but the most widely used way is this.
System.Reflection.Assembly.GetEntryAssembly().Location;
In my nunit test project I have a layout like:
/Fixters/someFile.txt
/Services/SomeService.cs
someFile.txt is a file that I need to read as it contains text that I use for my unit tests.
How can I reference this folder without hard coding the path as other team members may store this project at a different path that me.
If you do File.ReadAllText(filepath) and any other equivalent method where you pass in a relative path, the runtime will use the app's current working directory to create the whole path. So generally speaking, you should be fine using relative paths. You might also need to specify that someFile.txt gets copied to the output directory.
If unsure what the cwd is, try Directory.GetCurrentDirectory()
I have a class in C# that saves an error message in a log file in case of an exception. Now I want to save the log file in the same folder containing the application's (in my case, a website) files. I tried using Environment.CurrentDirectory however it is not retrieving the path to my website. What can I do please to make use of a relative file path which points inside the website's directory?
Here is the class' code. As you can see, the path is absolute. I want to change it to a relative file path pointing to a folder in my website's directory.
Usually Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) returns the path where the current assembly resides. You could use
string logName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MyLogFile.log");
to create the log file name.
Question is really whether logging to the application's folder is permitted by the OS. Also, for Web-applications, the log file would be publically visible and accessible through the web browser.
For a website use:
HttpContext.Current.Server.MapPath("~/");
You might also could try this solution:
string path = AppDomain.CurrentDomain.BaseDirectory + "anotherFolder";
This would put the base dir of the app and a folder inside of the project!
I included a text file as output in a WCF set up project.
The text file is correctly located in the same folder with the dll, exe, and config file after the project is installed (C:\Program Files\KLTesting\KLTestApp).
However when the program tries to read it, it looks under "C:\Windows\system32", what's the correct way to find & read it?
I have
string a = Directory.GetCurrentDirectory();
a += "/R0303.txt";
string content = File.ReadAllText(a);
Thanks.
You should use AppDomain.CurrentDomain.BaseDirectory or AppDomain.CurrentDomain.SetupInformation.ApplicationBase instead, to get the Directory of your .exe file.
First you should not call Directory.GetCurrentDirectory() and concatenate with the file name. The service is running within a web server like IIS or some other type of container, so GetCurrentDirectory will not give you what you are thinking as you found out. (On a quick tangent, as a recommendation in the future if you want to do path combining you should use Path.Combine method as it will handle all the corner cases and be cross platform.)
There are a few ways to do this:
Assembly myAssembly = Assembly.GetAssembly(SomeTypeInAssm.GetType());
string fullFileName = Path.Combine(myAssembly.Location, "MyFile.txt");
The fullFileName should be what you are looking for. Make sure you use a type that is actually in an assembly located and referenced from the directory in question. However be aware because this file in your question in the Program Files area this is a secure area on Vista and higher OS's and you may not have access to do anything but read the file.
Also you could have the installer of the application place in the registry the install path. Then your service if on the same box can pull this information.
Also you could make sure the file you want to load is within the web server environment that is accessable to the WCF service in question.
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.