[英]How can I get the location of a user's Documents directory?
I want to be able to write a string to a text file on any computer. 我希望能够在任何计算机上将字符串写入文本文件。 For example
例如
string line = "Sample text to write"
File.WriteAllText(@"C:\Users\Rolex James\Documents\sample.txt" , line);
On another system this path would be different, is there a way of writing it such that you don't have to modify the path to suit each system you want to run it on? 在另一个系统上,这条路径会有所不同,有没有办法编写它,这样你就不必修改路径来适应你想要运行它的每个系统?
It depends on where you want to write it, which isn't completely clear by your question. 这取决于你想写它的位置,你的问题并不完全清楚。 If you're always looking for the current user's documents, then you should look at the
System.Environment.GetFolderPath
method and the System.Environment.SpecialFolder.MyDocuments
enumeration value in particular. 如果您一直在寻找当前用户的文档,那么您应该特别查看
System.Environment.GetFolderPath
方法和System.Environment.SpecialFolder.MyDocuments
枚举值。 When that executes, it will give you the path to the current user's documents directory, which will vary from user to user and computer to computer. 执行时,它将为您提供当前用户文档目录的路径,该目录因用户和计算机而异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.