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.
Asked
Active
Viewed 3.8k times
18

Steve
- 213,761
- 22
- 232
- 286

Matt Hatcher
- 663
- 1
- 4
- 17
2 Answers
47
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:

Steve
- 213,761
- 22
- 232
- 286
-
Sorry about the double post... Okay, thank you Steve. Now that I know this is possible, let me get more specific. I only need it to create once. So, I am making a loading screen that will check for all the required things, like the appdata folder and such. I need it to check and if that folder has been created, it will pass over. If not, it will create it. I know this will be an automatic thing. So. Yeah. Also, the file I would copy and replace is all in the application folder. Also, I would need to rename the file that I am copying. The name has to be the same. – Matt Hatcher May 11 '13 at 18:29
-
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`? – Steve May 11 '13 at 19:08
-
Okay the easiest example I can think of is Minecraft. So, minecraft.jar is located in the appdata .minecraft/bin/minecraft.jar. Let's say I want to replace that minecraft.jar with my own, but what if mine is named _minecraft.jar, that won't work. I would need have it change the file name. So it copies appdata/.myfolder/_minecraft.jar and renames and replaces the appdata/.minecraft/bin/minecraft.jar. Does that make sense? – Matt Hatcher May 11 '13 at 19:53
-
Also, it is giving me this.... http://i.imgur.com/YeoieFs.png – Matt Hatcher May 11 '13 at 19:59
-
Add `using System.IO;` to the beginning of your file – Steve May 11 '13 at 20:01
-
Oops, thought it was there. Thank you :) Also, I need help with database things, but I'll post later about that. – Matt Hatcher May 11 '13 at 20:02
-
But let me ask this, I was wondering. I am creating something for Minecraft. But I don't want people just using it. I want to have a login that verifies that they have a Minecraft account, how would I do that? – Matt Hatcher May 11 '13 at 20:05
-
Can't help with that, if you post a new question you will receive renewed attention – Steve May 11 '13 at 20:07
1
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).

Tadas Šukys
- 4,140
- 4
- 27
- 32