0

I have created a C# Console App for use in Command Prompt.

Here is my code:

static void Main(string[] args)
{
    string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    Console.WriteLine(path);
}

My result: enter image description here

I want to get C:\Users\Sophairk instead of C:\testswc.

How to fix my code to get as what I want?

Thanks in advance!

Sophairk
  • 253
  • 3
  • 11

1 Answers1

1

When you call Assembly.GetEntryAssembly().Location then it will return the path of the executing .exe file.

Assuming that Sophairk is the username of your user, then you can do this:

 string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

If it is the current directory that you want, then you can do this

string path = Directory.GetCurrentDirectory();

You can check out the documentation here: https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-6.0

To see what other "Special Folders" are available.

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51