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);
        }
    }
}       
8
  • Have you tried Application.StartupPath? Commented May 23, 2019 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. Commented May 23, 2019 at 12:55
  • Directory.GetCurrentDirectory()? Commented May 23, 2019 at 12:55
  • @steve16351 When I use Directory.GetCurrentDirectory(), it gives me C:\WINDOWS. Commented May 23, 2019 at 12:57
  • See How can I get the application's path in a .NET console application? Commented May 23, 2019 at 13:00

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Typing Environment.GetCurrentDirectory() tells me that Environment does not contain GetCurrentDirectory. And Environment.CurrentDirectory returns C:\WINDOWS regardless of where I execute my command.
Why Environment? Use System.IO.Directory.GetCurrentDirectory()
Sorry, I misread it. Directory.GetCurrentDirectory() works, that is exactly what I was looking for. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.