C# 如何在“中创建文件文本”;AppData\Roaming“;对于C中的任何用户#

C# 如何在“中创建文件文本”;AppData\Roaming“;对于C中的任何用户#,c#,C#,我想保存我的应用程序的日期,但我不知道应用程序是否为不同的用户保存,例如,如果“andy”我不能使用(“C:\users\Game maker\AppData\Roaming”),那么如何在“AppData\Roaming\MaxrayStudyApp”中为任何用户创建文件 Computer myComputer = new Computer(); myComputer.FileSystem.WriteAllText(@"C:\Users\Game maker\AppDa

我想保存我的应用程序的日期,但我不知道应用程序是否为不同的用户保存,例如,如果“andy”我不能使用(“C:\users\Game maker\AppData\Roaming”),那么如何在“AppData\Roaming\MaxrayStudyApp”中为任何用户创建文件

    Computer myComputer = new Computer();
        myComputer.FileSystem.WriteAllText(@"C:\Users\Game maker\AppData\Roaming\MaxrayStudy\data.txt","", false);

几乎是这个的复制品:

这将获得所需的文件夹,然后使用Path.Combine()写入该文件夹中的目录

var roamingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filePath = Path.Combine(roamingDirectory, "MaxrayStudy\\data.txt");

我重新启动我的windows,然后写下这个

string filePath =  Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        filePath= (filePath+@"\MaxrayStudyApp\data.txt");
        string path =Convert.ToString(filePath);
        using (System.IO.StreamWriter file = 
    new System.IO.StreamWriter(filePath,false)){
            file.WriteLine(" XP : 0");}

查找可能重复,如果遇到问题,请发布您尝试过的代码和遇到的具体问题。感谢您的回答我写了3行,但我没有找到“data.txt”文件:(可能没有权限。很难说。你能在那里手动创建一个文件并用记事本之类的东西写入吗?是的,我可以,我以管理员的身份运行我的应用程序,但它不起作用。是它引发了一个错误吗?你必须确保目录存在。签出CreateDirectory()方法,然后尝试写入该文件。
string filePath =  Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        filePath= (filePath+@"\MaxrayStudyApp\data.txt");
        string path =Convert.ToString(filePath);
        using (System.IO.StreamWriter file = 
    new System.IO.StreamWriter(filePath,false)){
            file.WriteLine(" XP : 0");}