zoukankan      html  css  js  c++  java
  • [C#]批量修改文件后缀名

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ 
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10741596.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    代码:

     1 /// <summary>
     2 /// 修改文件后缀
     3 /// </summary>
     4 /// <param name="extension">文件后缀</param>
     5 private void UpdateExtension(string extension)
     6 {
     7     //弹框选择文件夹
     8     FolderBrowserDialog dialog = new FolderBrowserDialog
     9     {
    10         Description = "请选择文件夹"
    11     };
    12     if (dialog.ShowDialog() == DialogResult.OK)
    13     {
    14         //获得文件夹路径
    15         string foldPath = dialog.SelectedPath;
    16         if (!string.IsNullOrEmpty(foldPath))
    17         {
    18             //初始化文件夹对象
    19             DirectoryInfo dir = new DirectoryInfo(foldPath);
    20             // 获取当前文件夹下的所有文件
    21             //TopDirectoryOnly:在搜索操作中包括仅当前目录
    22             FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly);
    23             //遍历当前文件夹下的所有文件
    24             for (int i = 0; i < files.Length; i++)
    25             {
    26                 //获取并输出文件扩展名称
    27                 Console.WriteLine(Path.GetExtension(files[i].FullName));
    28                 //修改文件扩展名称
    29                 files[i].MoveTo(Path.ChangeExtension(files[i].FullName, extension));
    30                 //获取并输出文件扩展名称
    31                 Console.WriteLine(Path.GetExtension(files[i].FullName));
    32             }
    33         }
    34     }
    35 }

    测试:

    1 //注意不需要加'.'
    2 UpdateExtension("swift");
  • 相关阅读:
    九度oj 题目1371:最小的K个数
    九度oj 题目1131:合唱队形
    九度oj 题目1450:产生冠军
    九度oj 题目1135:字符串排序
    九度oj 题目1534:数组中第K小的数字
    九度oj 题目1179:阶乘
    九度oj 题目1369:字符串的排列
    九度oj 题目1100:最短路径
    [Luogu] 子串
    [Luogu] 魔法树
  • 原文地址:https://www.cnblogs.com/strengthen/p/10741596.html
Copyright © 2011-2022 走看看