简体   繁体   English

如何获取用户文档目录的位置?

[英]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.

相关问题 如何让CommonOpenFileDialog的InitialDirectory成为用户的MyDocuments路径,而不是Libraries \\ Documents? - How can I get the CommonOpenFileDialog's InitialDirectory to be the user's MyDocuments path, instead of Libraries\Documents? 如何获取当前用户目录? - How can I get the current user directory? 如何获取TextBlock的TextTrimming的位置 - How can I get the location of a TextBlock's TextTrimming 如何在WPF中获取按钮在画布上的位置? - How can I get a button's location on the canvas in WPF? 如何允许应用程序用户选择可以保存应用程序文档的文件夹位置 - How to allow app user to select a folder location where in app documents can be saved 如何在C#中获取Mac OSX当前用户的目录? - How do I get Mac OSX current user's directory in C#? 如何从数据库中获取Google Map用户的位置? - How to get google map user's Location from the database? 如何在与机器人聊天时获取用户的位置? - How to get user's location while chatting with bots? 我如何获取文本文件的位置 - How can i get the location of a textfile 如何获取网格上某个位置的坐标? - How can I get the coordinates of a location on the grid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM