How to create appdata folder with C#
Asked Answered
S

2

21

I basically need to create a folder in the roaming application data of the current user running the program.
Then I also need to access another folder in the application data section where I have a file that I want to copy into the application data folder I had just created.

Sourdine answered 11/5, 2013 at 17:49 Comment(0)
E
49

The first two passes are straightforward

// The folder for the roaming current user 
string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

// Combine the base folder with your specific folder....
string specificFolder = Path.Combine(folder, "YourSpecificFolder");

// CreateDirectory will check if every folder in path exists and, if not, create them.
// If all folders exist then CreateDirectory will do nothing.
Directory.CreateDirectory(specificFolder);

In the last pass is not clear where you have the file to copy.
However, supposing that you have a file called

string file = @"C:\program files\myapp\file.txt";
File.Copy(file, Path.Combine(specificFolder, Path.GetFileName(file));

MSDN links:

Path class
Environment.SpecialFolder enum
File.Copy method

Eyeball answered 11/5, 2013 at 17:58 Comment(3)
OK I have added a check for the CreateDirectory. For the file copy part, again, you need to give an example on how your file to copy is named, how do you want to rename it and where it is located. What do you mean with application folder?Eyeball
Add using System.IO; to the beginning of your fileEyeball
Can't help with that, if you post a new question you will receive renewed attentionEyeball
T
2

I would suggest you to use Isolated Storage without bothering where your files are physically located. That's more flexible way - you just use the Isolated Storage API and the .NET framework is responsible where physically files are located (for example in different operating systems location may be different).

Tendance answered 11/5, 2013 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.