2

I'd like save date my app but I'm not knowing if app save for different users, Example if "andy" I can't use ("C:\Users\Game maker\AppData\Roaming") ,So How to create file in "AppData\Roaming\MaxrayStudyApp" for any user .

    Computer myComputer = new Computer();
        myComputer.FileSystem.WriteAllText(@"C:\Users\Game maker\AppData\Roaming\MaxrayStudy\data.txt","", false);
Cœur
  • 37,241
  • 25
  • 195
  • 267
MAX.RAY
  • 21
  • 1
  • 6
  • 2
    Possible duplicate of [How can i get the path of the current user's "Application Data" folder?](http://stackoverflow.com/questions/915210/how-can-i-get-the-path-of-the-current-users-application-data-folder) – Martin Sep 14 '16 at 19:04
  • Look up https://www.google.com/#q=c-sharp+environment.specialfolder and if you get stuck, post the code you've tried and the specific problem you're having. – Shannon Holsinger Sep 14 '16 at 19:29

2 Answers2

5

Almost a duplicate of this one: Environment.SpecialFolder.ApplicationData returns the wrong folder

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

That should get the folder you need then use Path.Combine() to write to a directory in that folder.

var roamingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filePath = Path.Combine(roamingDirectory, "MaxrayStudy\\data.txt");
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
KSib
  • 893
  • 1
  • 7
  • 24
  • thanks for answer me I write the 3 lines but I didn't find "data.txt" file :( – MAX.RAY Sep 14 '16 at 19:25
  • Might not have permissions. Hard to say. Can you create a file there manually and write to it with notepad or something? – KSib Sep 14 '16 at 19:27
  • yes I can and I run my app As adminaster but it isn't working – MAX.RAY Sep 14 '16 at 19:53
  • Is it throwing an error? You have to make sure the directory exists. Check out the CreateDirectory() method then try to write the file. – KSib Sep 14 '16 at 20:01
0

I restart my windows and I write this

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");}
MAX.RAY
  • 21
  • 1
  • 6