Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create appdata folder with C# [closed]

Tags:

c#

appdata

Well, I don't know how to type all this so bear with me please.

This is beyond me, I'm still a newb at C#. I basically need to create a folder in the roaming application data of the current user running the program. I also need to access another folder in the application data section and then replace a file with a copy of the file in the application data folder I had created.

like image 819
Matthew H Avatar asked May 11 '13 17:05

Matthew H


People also ask

How do I make an AppData folder?

Select File Explorer Options. Select the View tab of the File Explorer Options window. Choose Show hidden files, folders, and drives > Apply > OK. The AppData folder is located at C:\users\YOURNAME, where YOURNAME is your Windows profile ID.

How do I move AppData to C drive?

Double-click AppData in the 1 and 2 directories and change the path to the folder directory you want to transfer in the pop-up box. Then copy all the data in the original AppData directory to whichever directory you modified, and finally restart your computer.

Why is there no AppData folder?

Normally, you will not find the AppData folder in your User Profile page because it is hidden by default. We will change the hidden settings and then access the file location. Make sure that you are logged in as an administrator.

Can I delete everything in AppData folder?

You want to delete the contents of AppData folder. The AppData folder would have data regarding the applications in the computer. If its contents are deleted, data would be lost and you may not be able to use some applications as well.


1 Answers

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

like image 139
Steve Avatar answered Oct 14 '22 09:10

Steve