How to create appdata folder with C# [closed]

你。 提交于 2019-11-27 02:41:59

问题


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.


回答1:


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 folder exists and, if not, create it.
// If folder exists 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




回答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).



来源:https://stackoverflow.com/questions/16500080/how-to-create-appdata-folder-with-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!