Command line current working directory in a console app [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I find out what directory my console app is running in with C#?
How to get current working directory of a console that runs a program so I could resolve relative paths passed as program args?
Lets say I've put my program here: c:\tools\program.exe
But I'm invoking it from various places. Lets say I'm here: C:\Users\Me\Documents\ and I run this command program.exe --src=somefile.txt --dest=subdir\otherfile.txt
Environment.CurrentDirectory and System.Reflection.Assembly.GetExecutingAssembly().Location will return c:\tools but I would like to be able to resolve somefile.txt and subdir\oterfile.txt paths that are relative to C:\Users\Me\Documents\.
====== UPDATE ======
Thank you for your help. It seems that Environment.CurrentDirectory works as expected. It turned out that in my case the problem was caused by Xenocode's Postbuild tool (now called Spoon Virtual Application Studio) that I occasionally use to "pack" all program's files (including dlls, config, etc.) into one executable. It's very handy, but in this case the "virtualization" feature messed up my program's Environment variables. I've managed to solve that issue.

Environment.CurrentDirectory gives you the Current Working Directory
Let me show a simple example:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Startup: " + Environment.CurrentDirectory);
Environment.CurrentDirectory = #"D:\temp";
Console.WriteLine("After:" + Environment.CurrentDirectory);
}
}
Now I create two folders named D:\temp and D:\temp2
I put the executable in D:\temp
Open a command prompt and set the current working directory with cd D:\temp2
From that directory I run ..\temp\mytestapp.exe
the output is
Startup: D:\temp2
After: D:\temp
As a curiosity:
this was the documentation for Environment.CurrentDirectory in Net 1.1
Gets and sets the fully qualified path of the current directory; that
is, the directory from which this process starts.
and this is the documentation in NET 4.0
Gets or sets the fully qualified path of the current working
directory.

Use the Environment and Path classes. http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx
var fqn = Path.Combine(Environment.CurrentDirectory, "somefile.txt")
But to play with your documents you need:
var fqn = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Person),
"somefile.txt");
`fqn' is TLA for fully qualified name

Related

Mono executes program with wrong current directory

I have a strange problem. I have written a custom spamfilter in C# .NET 4.6 for personal use. And I put this program up to my Raspberry Pi.
I have tested many times the program, and everything worked fine, but when I created a cronjob I have noticed that the program never writes log.
As I started to test again I have find out, that the problem is, when I start the program from the program's base dir, it works fine:
> cd path/to/program
> mono program.exe
but when I start it from another directory with absolute path:
> mono /absolute/path/to/program/program.exe
The required files' relative paths go wrong.
For example if I'm in the /home directory, and my program is in /home/user/program directory, and I run this:
> mono /home/user/program/program.exe
I get " Could not find file "/home/xyz.txt" "
I have tried to create absolute paths from relative paths in the program using Path.GetFullPath() and Environment.CurrentDirectory but nothing changed.
So my question is: Is there any option to run a mono program in another directory with correct current directory?
I know that a bash script could execute my first solution (cd and then mono) and it could be used in cron, but I want to know if there is a simplier solution.
The current directory is correct, it is the current directory of where you are starting the process, and that process is mono in this case, and not the location of the CIL-based assembly that mono is loading.
What it sounds like you want is the path of the ExecutingAssembly.
This works on x-plat (Windows/Linux/macOS):
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
Console.WriteLine(path);
Ref: https://msdn.microsoft.com/en-us/library/aa457089.aspx

Getting path of working directory [duplicate]

This question already has answers here:
Get current folder path
(13 answers)
Closed 8 years ago.
I need to access working directory from code in C# in MSVS 2013 like:
C:\SomePath\Visual\NameOfTheProject\
In java we can access it System.getProperty("user.dir")
Does C# have equivalent?
#update
Problem is that I need to search folder X for implementations of some interface. App will be executed on different machines, so I should have a path which will always navigate me to folder X
#update2
I need to look for the folder with dlls which is named X in the folder where I execute the app (so my assembly).
Directory.GetCurrentDirectory() will return the current working directory of the process. That is the equivalent of the "user.dir" property in Java.
To get the directory of the exe (or actually, the entry assembly, which is normally the exe), use Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
Path.GetDirectoryName
Assembly.GetEntryAssembly()
Assembly.Location
To get the absolute path to the executable file (And not where it was executed from, if it was through a shortcut) you can use:
System.IO.Path.GetDirectoryName(Application.ExecutablePath); (Needs to be using Windows Forms)
or
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Both will return the directory that your executable is found in.
To get the path of another folder within your executable's directory, you can use this:
Path.Combine(path, "X");
Where path is any of the above examples.

What is the difference between these ways of getting current directory?

They all give the same result, the location of folder that contains the exe that is being executed. I am sure there are no good or bad methods in the .net BCL. They are all appropriate in particular circumstances. Which one is appropriate for which scenario?
var appBaseDir = AppDomain.CurrentDomain.BaseDirectory;
var currentDir = Environment.CurrentDirectory;
var dir = Directory.GetCurrentDirectory();
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
They all give the same result
They certainly don’t. currentDir and dir both give you the current working directory – i.e. by default the directory your executable was run from (but it can be changed during the execution).
By contrast, appBaseDir and path get the directory which contains the executing assembly’s file.
To illustrate how they differ, consider that you have an executable which sits in C:\bar\baz.exe. Now I can execute the application by entering the following chain of commands in a terminal:
$ md C:\foo
$ cd C:\foo
$ ..\bar\baz.exe
Now the current working directory is C:\foo but the application’s base directory is C:\bar. There exist analogous means of setting the working directory for other methods of launching an application (e.g. via a shortcut icon or programmatically, such as via Process.Start).
Still, the framework provides different ways of accessing this information:
Environment.CurrentDirectory quite directly conveys the meaning that the execution environment (an environment variable) is queried. Directory.GetCurrentDirectory() may actually do the same internally (I have no idea) but it encapsulates this, and rather focuses on providing the user with a logical API for querying information about directories.
AppDomain.CurrentDomain has information about the current AppDomain (roughly, the executable). Part of that information is, logically, the AppDomain’s path. By contrast, System.Reflection.Assembly gives you general information about assembles – these represent any kind of binary objects in .NET, including DLLs and EXEs. GetExecutingAssembly in particular returns the currently executed assembly. And you can get its path again by querying its Location property, which gives the physical path of an assembly file.
Consider the example above
The myTest.exe file contains the entry point and is at located at D:\myTest.exe. This exe calls via reflection a method in an assembly in F:\. This assembly contains all the find directory code.
In the command prompt I have my current directory set to C:\
Here are the results
AppDomain.CurrentDomain.BaseDirectory
D:\
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
F:\
Environment.CurrentDirectory and Directory.GetCurrentDirectory()
C:\
AppDomain.CurrentDomain.BaseDirectory will give you the directory the application is running in.
Environment.CurrentDirectory & Directory.GetCurrentDirectory can change during the execution of an application. You can see the behavior if you get the value at the start of execution, then use something like OpenFileDialog, then then get the value again. You will notice that the value will have changed to where the OpenFileDialog was pointing.
I found with .net 5 that if the application is published as a self-contained single-file EXE, all of these well-known methods to get the EXE location return an empty string:
System.Reflection.Assembly.GetExecutingAssembly().Location
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
typeof(Program).Assembly.ManifestModule.FullyQualifiedName
However this does work to get the containing folder path:
AppDomain.CurrentDomain.BaseDirectory

Directory.GetCurrentDirectory() not working on linux?

So I'm trying to create an application that requires the reading of scripts in a sub-folder called "scripts". My code I'm having issues with:
string script = Console.ReadLine();
string path = Directory.GetCurrentDirectory();
string sciptpath = path + "/scripts/" + script;
This works fine on Windows. But on Linux (running using Mono Runtime) it goes to the current users home Directory...not the directory of the executable. Is this a bug? And can someone suggest a workaround?
It's not that it needs "fixing" it's that the current directory is not what you think it is. The current directory is the directory that "has focus" for relative paths. Regardless where your EXE is, your current directory can be anywhere else, or may even change during execution.
What you want is:
string path = Path.GetDirectoryName(Application.ExecutablePath);

Better way to get the base directory?

I have this code to load a config file and read all the values and it works fine when running the application but of course fails on team city because the appdomain's base directory is where the build script (psake) is started. I know I can change directory to the build dir before executing the tests but I thought it's better to make loading the config file work at all times regardless.
XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cfgFile));
Is there another way to get the "BaseDirectory" that really works all times? I tried the below as well with same results:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
XDocument.Load(Path.Combine(path, cfgFile));
EDIT 1 The problem is the following. My solutions base directory is "C:\Project", all compiled files are copied to "C:\Project\build". Now in the psake build script I have the following code:
task Test -depends PrepareTests {
try {
#$old = pwd
#cd $build_dir
&$mspec $mspec_projects --teamcity
#cd $old
}
catch {
&echo "Error starting test runner"
Exit 1;
}
}
As you can see I commented out the changing of directories which makes the BaseDirectory the location of the build file / solution instead of the build directory regardless of how I try to access it. Kind of confusing if you ask me.
UPDATE I really like to know if it is possible to get the directory of the assembly regardless of what directory the application that started the app domain is located. How?
differents ways to get the base directory
AppDomain.CurrentDomain.BaseDirectory
Directory.GetCurrentDirectory() // not guaranteed to work on Mobile application
Environment.CurrentDirectory // this calls Directory.GetCurrentDirectory()
this.GetType().Assembly.Location // Assembly.location
Application.StartupPath // for windows forms apps
Application.ExecutablePath // same as Application.StartupPath
string origAssemblyLocation = Assembly.GetExecutingAssembly().CodeBase;
Per MSDN:
Assembly.CodeBase Property
Gets the location of the assembly as
specified originally
Your question is a bit unclear. I don't really know if this is what you want.
I typically use AppDomain.CurrentDomain.BaseDirectory
Alternatives
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Environment.CurrentDirectory
So it sounds/looks like you're attempting to obtain the configuration file for an assembly. The following should accomplish that task by accessing the 'Location' property of the assembly and using it to retrieve the configuration path:
static string GetConfigFileByType(Type type)
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(type.Assembly.Location);
if (config.HasFile)
return config.FilePath;
throw new FileNotFoundException();
}
try this one:
Module[] modules = Assembly.GetExecutingAssembly().GetModules();
return Path.GetDirectoryName(modules[0].FullyQualifiedName);
have you tried getting the FileName of the current process' MainModule?
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
Or GetEntryAssembly()?
System.Reflection.Assembly.GetEntryAssembly().Location
I like to make my classes configurable - for example they get the folder name as a parameter in their constructor.
This makes it possible to test with different config files.
In test code we use:
TestContext.TestDeploymentDir
This is the testrun folder, where all assemblies for a test run are copied into, together with the test deployment items. We 'deploy' our unit test config files to the test run folder - this can be specified in the testrunconfig dialog in visual studio.
For our production code we pass
Assembly.GetExecutingAssembly().Location
to the constructor, which works for us.
how about:
Application.StartupPath;
string baseDirectory=Application.StartupPath.Split(Path.DirectorySeparatorChar)[0];
Application startup path will return the path where exe is kept, we will split this using "\" and get the base directory as "C:" for example,

Categories

Resources