如何用C#创建appdata文件夹

问题描述 投票:0回答:2

我基本上需要在运行该程序的当前用户的漫游应用程序数据中创建一个文件夹。
然后,我还需要访问应用程序数据部分中的另一个文件夹,其中有一个文件,我想将其复制到刚刚创建的应用程序数据文件夹中。

c# appdata
2个回答
49
投票

前两关很简单

// 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 枚举
文件.复制方法


2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.