Reputation: 193432
I created the following .NET console app, built it, and copied the executable deepdir.exe
to c:\commandlineapps
and then set an enviroment variable to this directory so that I can call this command from any directory.
How do I get the directory from where the user typed the command, e.g. c:\docs\project1
, and NOT the directory where the .exe file exists, e.g. c:\commandlineapps
? None of the following work:
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
namespace showimages
{
class Program
{
static void Main(string[] args)
{
var docPath = AppDomain.CurrentDomain.BaseDirectory;
docPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
docPath = Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");
docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
docPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
docPath = Environment.CurrentDirectory;
docPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
docPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
docPath = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
docPath = System.AppContext.BaseDirectory;
docPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Console.WriteLine(docPath);
}
}
}
Upvotes: 1
Views: 251
Reputation: 11871
Directory.GetCurrentDirectory() will do what you need (get the current working directory).
Upvotes: 1