如何使用 C# 创建 appdata 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16500080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:10:30  来源:igfitidea点击:

How to create appdata folder with C#

c#appdata

提问by Matthew H

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.

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

采纳答案by Steve

The first two passes are straightforward

前两个pass很简单

// 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:

MSDN 链接:

Path class
Environment.SpecialFolder enum
File.Copy method

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

回答by Tadas ?ukys

I would suggest you to use Isolated Storagewithout 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).

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