17

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.

4

2 回答 2

44

前两遍很简单

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

在最后一关不清楚你有文件要复制的地方。
但是,假设您有一个名为

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

MSDN 链接:

路径类
Environment.SpecialFolder 枚举
File.Copy 方法

于 2013-05-11T17:58:15.877 回答
0

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

于 2013-05-11T20:15:01.737 回答