如何用C#创建appdata文件夹

时间:2013-05-11 17:49:52

标签: c# appdata

好吧,我不知道如何输入所有这些,所以请耐心等待。

这超出了我的范围,我仍然是C#的新手。我基本上需要在运行程序的当前用户的漫游应用程序数据中创建一个文件夹。我还需要访问应用程序数据部分中的另一个文件夹,然后用我创建的应用程序数据文件夹中的文件副本替换文件。

2 个答案:

答案 0 :(得分:39)

前两个传球很简单

// 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);

在最后一遍中并不清楚您要复制文件的位置 但是,假设您有一个名为

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

MSDN链接:

Path class
Environment.SpecialFolder enum
File.Copy method

答案 1 :(得分:1)

我建议您使用Isolated Storage,而不必担心文件的实际位置。这是更灵活的方式 - 您只需使用Isolated Storage API,.NET框架负责物理文件所在的位置(例如,在不同的操作系统位置可能不同)。

相关问题