简体   繁体   中英

C#: How to make a string with a path of the Current User's directory?

So basically I need a string "path" to be something like:

string path = @"C:\Users\CURRENT_USER\file.txt";

How could I do that?

Sincerely,

a guy with an internet connection

Your User profile path stores in

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

To get specifc file path from that Folder you need to combine it with hard coded string of your file name, like

 string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "file.txt")

Output:

在此处输入图片说明

Note: Do not forget to add using System.IO because Path class is present in System.IO

您可以使用Environment.SpecialFolder.UserProfile枚举。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM