John Johnson
John Johnson

Reputation: 1

How to add a users name to path in C#?

Hello stackoverflow community, I want to know how I would be able to do this code: Directory.Delete(@"C:\NAME\AppData\Local", true);But where it says "NAME" I want to get the username of the computer, is this possible? Thanks.

Upvotes: 0

Views: 152

Answers (3)

MKR
MKR

Reputation: 20095

I hope you are looking for a way to replace "NAME" with current user logged on that computer. If so then you can use Environment.UserName. Environment.UserName will return you the user on the current thread.

Upvotes: 1

Ayaz
Ayaz

Reputation: 2121

Try this.

Directory.Delete(string.Format(@"C:\{0}\AppData\Local",Environment.UserName), true);

As per Alex comment, if you want to delete "C:\USERS\NAME\AppData\Local" then use this code.

var directory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApp‌​licationData);
Directory.Delete(directory, true);

Upvotes: 0

ahmeticat
ahmeticat

Reputation: 1939

You can use, Getting the computer name in windows

and

string directory = "C:\"+username+"\AppData\Local"; Directory.Delete(@directory, true);

Upvotes: 0

Related Questions