HA

C#重命名文件夹下的文件

C#重命名文件夹下的文件

            var files = System.IO.Directory.EnumerateFiles(desPath);
            if(formatStr.Length == 0) {
                formatStr = "{0}.jpg";
            }
            int count = 1;
            foreach(String fileName in files) {
                if (new System.IO.FileInfo(fileName).Length > 1024 * 75) {
                    System.IO.File.Move(fileName, System.IO.Path.Combine(desPath, String.Format(formatStr, count)));//对文件进行重命名
                    count++;
                }
            }

判断系统是否是win10

        bool IsWindows10() {
            var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
            string productName = (string)reg.GetValue("ProductName");
            return productName.StartsWith("Windows 10");
        }

获取桌面目录路径

        String GetDesktopDirectory() {
            return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        }

创建新文件夹

        void NewDirectory(String path) {
            if(!System.IO.Directory.Exists(path)) {
                System.IO.Directory.CreateDirectory(path);
            }
        }

base64编码

        String GetBase64String() {
            return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString()));
        }

获取win10锁屏壁纸的缓存目录路径

        public String GetWallPaperDirectory() {
            String desPath = "";
            String doc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            doc = doc.Substring(0, doc.LastIndexOf("\\"));
            String desTemp = System.IO.Path.Combine(doc, "AppData\\Local\\Packages");
            var files = System.IO.Directory.EnumerateDirectories(desTemp);
            foreach(String t in files) {
                if(t.Contains("Microsoft.Windows.ContentDeliveryManager")) {
                    desPath = t;
                    break;
                }
            }
            if(desPath.Length > 0) {
                desPath = System.IO.Path.Combine(desPath, "LocalState\\Assets");
                return desPath;
            }
            return "";
        }

将文件复制并重命名到新路径

System.IO.File.Copy(fileName, System.IO.Path.Combine(newPath, String.Format(formatStr, count)));

修改控件字体,可以通过设置窗体的字体实现,因为继承关系,所以子控件的字体也会改变

ps : 链接10里面的一个评论很搞笑, 说答主用StartsWith("Windows 10");不正确, 在Windows 100上判断会出错, 很有道理

pps : 感觉用C#写Demo比java方便, 主要是拖控件比较方便, 事件方法就在那里, 找一下就行了.

reference

  1. Adding an existing project to GitHub using the command line

  2. Directory.GetFiles Method (String)

  3. How to get only filenames within a directory using c#?

  4. Rename existing file name

  5. How to rename a file in .NET?

  6. How do you get the file size in C#?

  7. C#中将字符串转成 BASE64 编码

  8. Scrollbar on a Label

  9. Windows 10 will catched as Windows 8 / Windows 8.1

  10. How can I detect if my app is running on Windows 10

  11. Detect Windows version in .net

  12. How to get the “friendly” OS Version Name?

  13. [C#获取Windows系统特殊文件夹的路径转]

  14. String Class

  15. code