1

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);
        }
    }
}       
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • Have you tried Application.StartupPath? – Steve Todd May 23 '19 at 12:50
  • @SteveTodd What namespace is that? In my C# console application, I get the error "The name `Application` does not exist in the current context." The only namespace it offers is `System.Net.Mime.MediaTypeNames` which doesn't have `StartupPath`. – Edward Tanguay May 23 '19 at 12:55
  • `Directory.GetCurrentDirectory()`? – steve16351 May 23 '19 at 12:55
  • @steve16351 When I use `Directory.GetCurrentDirectory()`, it gives me `C:\WINDOWS`. – Edward Tanguay May 23 '19 at 12:57
  • See [How can I get the application's path in a .NET console application?](https://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application) – RobertBaron May 23 '19 at 13:00
  • Sorry, Application is part of WinForms. Does Environment.CommandLine give you anything useful? – Steve Todd May 23 '19 at 13:03
  • @RobertBaron As advised in the selected answer on that post, I tried `Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)` but it gives me `c:\commandlineapps` and not `c:\docs\project1` which is where I typed the command. The last comment on the selected answer notes rightly, "I do not understand at all why this is marked as the correct answer. It does not work in all the scenarios." – Edward Tanguay May 23 '19 at 13:04
  • @SteveTodd `Environment.CommandLine` simply gives me `deepdir` or if I type in an argument e.g. `deepdir *.png`, then it returns `deepdir *.png`. – Edward Tanguay May 23 '19 at 13:07

1 Answers1

1

Directory.GetCurrentDirectory() will do what you need (get the current working directory).

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
  • Typing `Environment.GetCurrentDirectory()` tells me that `Environment` does not contain `GetCurrentDirectory`. And `Environment.CurrentDirectory` returns `C:\WINDOWS` regardless of where I execute my command. – Edward Tanguay May 23 '19 at 13:27
  • 2
    Why `Environment`? Use `System.IO.Directory.GetCurrentDirectory()` – steve16351 May 23 '19 at 13:29
  • 1
    Sorry, I misread it. `Directory.GetCurrentDirectory()` works, that is exactly what I was looking for. Thanks. – Edward Tanguay May 23 '19 at 13:33