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.

Developer technologies | C#
Developer technologies | 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.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Castorix31 91,976 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);
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.