I want to get the current working directory of my exe. Directory.GetCurrentDirectory() not returning working directory of the exe when deploying the application.
What is the way to get current working directory?
Hi
try AppDomain.CurrentDomain.BaseDirectory
try:
var path = Environment.CurrentDirectory
or:
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Related
I'm trying to set up selenium tests in nCrunch which outputs the tests to its own temp folder.
I'm getting the following error when I create the driver (new PhantomJSDriver()):
OpenQA.Selenium.DriverServiceNotFoundException : The PhantomJS.exe file does not exist in the current directory or in a directory on the PATH environment variable.
However I have checked and PhantomJS.exe does exist in the current directory (\bin\debug).
I then tried using new PhantomJSDriver(".\\") which should be the current directory and that does work.
What is the "current directory" Selenium is referring to in this message?
Rather than assuming ".\\", get the current working working directory by Directory.GetCurrentDirectory or System.AppDomain.CurrentDomain.BaseDirectory . Take a look at Get current folder path.
new PhantomJSDriver() will use your bin folder
if PhantomJS.exe does not exist there, try to find where it's located and insert full path in constructor
new PhantomJSDriver("real_path_to_PhantomJS.exe")
I want to be able to set default folder and file creating inside of the folder where the app is installed? Because this app will be used on multiple machines so I cannot specify path like C://Users/PcName/etc.. Is there any very simple way of doing it?
What you are trying to do is not advisable; if your application is installed using recommended default methods (following Microsoft guidelines) the app will be in a directory under C:\Program Files (or where the program files folder may be redirected) and the user that runs the app will not have write access to that directory, so the directory creation will fail.
That said, you cannot use the Environment.CurrentDirectory, because it may or may not be the directory where your application's executable files reside, neither CurrentDomain.BaseDirectory, because that is not significative too (documentation says it's the directory where the loader will search for assemblies, but that may or may not be the directory of your application's executable files).
Copying from this other answer, the correct way to find the directory of your assembly is
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Once you have the path, you can try to create the directory with System.IO.Directory.CreateDirectory() and a file with System.IO.File.WriteAllText() or its siblings, or any other standard method of creating files.
You may also want to use the newer Assembly.GetExecutingAssembly().Location property, and use Path.GetDirectoryName() on that.
This applies to both web and windows apps:
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
You can get the path for the executable using this code (most of the time, actually it returns the current working directory)
System.Environment.CurrentDirectory
If you only specify a path that doesn't start with a drive letter then the path will be relative to where the application is running. e.g. The following program will create a folder and file in the application's local folder.
class Program
{
static void Main(string[] args)
{
var installedLocation = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
var di = installedLocation.CreateSubdirectory("MyFolder");
File.WriteAllText(Path.Combine(di.FullName, "File.txt"), "This will be written to the file");
var installedPath = AppDomain.CurrentDomain.BaseDirectory;
var di2 = Directory.CreateDirectory(Path.Combine(installedPath, "MyFolder2"));
File.WriteAllText(Path.Combine(di.FullName, "File2.txt"), "This will be written to the file");
}
}
In my project \debug directory i have the program exe file for example:
test.exe
Now once i will run this test.exe from c:\
And in the second time i will copy the test.exe to d:\ and run it from there.
In my code i have this line:
string programFilesX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + "\\Diagnostic Tool\\7z.dll";
Instead the program files x86 how can i get each the directory from where im running the exe file ?
One way (sure fire way also in .Net CE) is
string path = Path.GetDirectoryName(
Assembly.GetEntryAssembly().GetModules()[0].FullyQualifiedName);
or
string path = Path.GetDirectoryName(
Assembly.GetEntryAssembly().Location);
This will prevent Shortcut's from setting the applications CurrentDirectory or StartupPath which could technically be different from it's execution path (ClickOne programs for example).
You can get the running directory by doing:
Application.StartupPath
You can read more about it here
Alternatively you can try Environment.CurrentDirectory but that may not yield you the results you want because of short cuts and other ways of getting to your file.
http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx
You can also do:
System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Or
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
I need to do something like this:
StreamReader reader =
new System.IO.StreamReader(#"C:\Program Files\The Awesome Program That I Made\awesomeloadablefile.ldf");
Except I don't know where the user has installed the program. How is my program supposed to know where the installed files are?
I am a noob, in case you hadn't noticed.
You can use Assembly.GetEntryAssembly().Location to get the path on disk of your executable, Path.GetDirectoryName to get the directory it's in, and then Path.Combine to combine the directory name with your file name in that directory. So:
StreamReader reader = new System.IO.StreamReader(Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"awesomeloadablefile.ldf"));
Try something like this.
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Something like Assembly.GetExecutingAssembly().Location should work.
You could try this:
File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "awesomeloadablefile.txt");
Assuming you know the directory structure relative to your executable, you can use Application.StartupPath:
string path = Path.Combine(Application.StartupPath, "awesomeloadablefile.ldf");
StreamReader reader = new System.IO.StreamReader(path);
This will get you a path to the exe directory. I'm assuming that's where you decided to put the file. Otherwise you can specify a had location for it in the installer. Are you using the Visual Studio installer?
Application.StartupPath
How can I find out the folder where the windows service .exe file is installed dynamically?
Path.GetFullPath(relativePath);
returns a path based on C:\WINDOWS\system32 directory.
However, the XmlDocument.Load(string filename) method appears to be working against relative path inside the directory where the service .exe file is installed to.
Try
System.Reflection.Assembly.GetEntryAssembly().Location
Try this:
AppDomain.CurrentDomain.BaseDirectory
(Just like here: How to find windows service exe path)
Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
This works for our windows service:
//CommandLine without the first and last two characters
//Path.GetDirectory seems to have some difficulties with these (special chars maybe?)
string cmdLine = Environment.CommandLine.Remove(Environment.CommandLine.Length - 2, 2).Remove(0, 1);
string workDir = Path.GetDirectoryName(cmdLine);
This should give you the absolute path of the executable.
Another version of the above:
string path = Assembly.GetExecutingAssembly().Location;
FileInfo fileInfo = new FileInfo(path);
string dir = fileInfo.DirectoryName;
Environment.CurrentDirectory returns current directory where program is running. In case of windows service, returns %WINDIR%/system32 path that is where executable will run rather than where executable deployed.
This should give you the path that the executable resides in:
Environment.CurrentDirectory;
If not, you could try:
Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName
A more hacky, but functional way:
Path.GetFullPath("a").TrimEnd('a')
:)