C#创建文件目录和文件名本地化

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








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值