简体   繁体   English

C#:如何使用当前用户目录的路径创建字符串?

[英]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: 所以基本上我需要一个字符串“ path”来像这样:

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 注意:不要忘记using System.IO添加,因为System.IO存在Path

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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