Environment.UserName

adrenalineispog 1 Reputation point
2021-02-04T01:59:55.99+00:00

I was just coding some C# stuff and I came across an issue.

My code is:
string desktop = @"C:\Users\" + Environment.UserName + @"\Desktop";
string localFilename = @"C:\Users\" + Environment.UserName + @"\Desktop\image.jpg";

I tried it like this and it worked fine:
string desktop = @"C:\Users\USERNAME\Desktop";
string localFilename = @"C:\Users\USERNAME\Desktop\fronk.jpg";

But I need it so others can use it, and therefore cannot use my username. Please could I have some help, thank you.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 83,206 Reputation points
    2021-02-04T03:09:47.437+00:00

    If you want to get the user Desktop folder, use :

    string sDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    
    1 person found this answer helpful.
    0 comments No comments