C#中为PC程序建立存储文件夹代码如下:
public static class FolderManager
{
// 要创建的本地文件夹名
public const string APP = "app";
public const string IMAGE = "photo";
public const string AUDIO = "music";
public const string VIDEO = "video";
public const string FOLDER = "folder";
public const string OTHER = "misc";
public const string BACKUP = "backup";
private static string[] paths = new string[] {
APP,
IMAGE,
AUDIO,
VIDEO,
FOLDER,
OTHER,
// BACKUP
};
private static string Folder = "";
private static string desktopFolder = "";
//文件夹路径集合
private static Dictionary<string, string> fullPaths = new Dictionary<string, string>();
public static Dictionary<string, string> SubFolder
{
get { return fullPaths; }
}
//创建文件夹
public static void SetFolder(string path)
{
Folder = path.TrimEnd('\\');
if(!System.IO.Directory.Exists(Folder))
{
//没有该文件夹时,创建该目录
System.IO.Directory.CreateDirectory(Folder);
}
foreach(string folder in paths)
{
//创建该路径下的子文件夹,分别为app,music...
fullPaths[folder] = Folder + '\\' + folder;
}
// create sub folders if neccessary
foreach(string value in fullPaths.Values)
{
if(!System.IO.Directory.Exists(value))
{
System.IO.Directory.CreateDirectory(value);
// make it system folder
}
}
在程序的路径下,添加一个关于语言选择的配置文件夹,根据程序的版本语言选择文件名的显示语言:
如App在中文版本下的配置文件为App_desktop.ini,放在 程序路径\\ zh-CN 内容如下:
[.shellclassinfo]
LocalizedResourceName=应用
选择语言的代码如下:
//获取当前运行程序所在的文件路径
desktopFolder =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//获取当前程序版本的语言类型
string lang = Thread.CurrentThread.CurrentUICulture.Name;
desktopFolder += "\\" + lang;
if(System.IO.Directory.Exists(desktopFolder))
{
// copy desktop.ini
foreach (string folder in paths)
{
string dpath = Folder + "\\" + folder + "\\desktop.ini";
string src = desktopFolder + "\\" + folder + "_desktop.ini";
if (System.IO.File.Exists(src) && !System.IO.File.Exists(dpath))
{
string dst = dpath;
System.IO.File.Copy(src, dst);
DirectoryInfo dirInfo = new DirectoryInfo(fullPaths[folder]);
dirInfo.Attributes = dirInfo.Attributes | FileAttributes.System;
System.IO.File.SetAttributes(dst, FileAttributes.Hidden | FileAttributes.System);
}
}
}