使用C#修改文件名

 

 

 string path = @"C:\Users\Administrator\Desktop\project\new\DanZhuTaiYu222";
            string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            string temp = "";
            string name = "";
            int i = 0;
            foreach (string s in files)
            {
                temp = Path.GetFileNameWithoutExtension(s);
                if (temp.Contains('.'))
                {
                    name = temp.Replace('.', '_');
                    File.Move(s, Path.GetDirectoryName(s) + "\\" + name + Path.GetExtension(s));
                }            
                Console.WriteLine(++i);
            }
            Console .ReadKey();

 

 

       public void Deleted()
        {
            DirectoryInfo di = new DirectoryInfo(@”C:\KuGou\”);
            FileInfo[] fi = di.GetFiles(“*.mp3”);
            foreach(FileInfo info in fi)
            {
                if(info != null)
                {
                    File.Delete(info.FullName);
                }
            }
        }

 

 

   /// <summary>
        /// 复制文件夹中的所有内容
        /// </summary>
        /// <param name=”sourceDirPath”>源文件夹目录</param>
        /// <param name=”saveDirPath”>指定文件夹目录</param>
        public void CopyDirectory(string sourceDirPath, string saveDirPath, string type)
        {
            try
            {
                if (!Directory.Exists(saveDirPath))
                {
                    Directory.CreateDirectory(saveDirPath);
                }
                string[] files = Directory.GetFiles(sourceDirPath);
                foreach (string file in files)
                {
                    if (file.Contains(type))
                    {
                        string pFilePath = saveDirPath + “\\” + Path.GetFileName(file);
                        if (File.Exists(pFilePath))
                            continue;
                        File.Copy(file, pFilePath, true);
                    }
                }

                string[] dirs = Directory.GetDirectories(sourceDirPath);
                foreach (string dir in dirs)
                {
                      CopyDirectory(dir, saveDirPath + “\\” + Path.GetFileName(dir), type);
                }
            }
            catch (Exception ex)
            {

            }
        }

 

文件写入

using System;
using System.IO;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\双扣小游戏\trunk\skclient\bin\res\sounds\Avatar\12";
            string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            string temp = ""; 
            int i = 0;
            FileStream fs = new FileStream("E:\\双扣小游戏\\trunk\\testWrite.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            foreach (string s in files)
            {
                temp = Path.GetFileNameWithoutExtension(s);
                // if (temp.Contains(","))
                {
                    //name = temp.Replace('.', '_');
                    //{ "Type": "sound", "Name": "Emotion_victory",     "Dir": "res/sounds/chat/",  "Path": "res/sounds/chat/Emotion_victory.mp3"},
                    string path1 = Path.GetFullPath(s); 
                    path1.Replace("\\", "/");
                    sw.WriteLine("{0}:{1}, {2}:{3},   {4}:{5}, {6} : {7} {8}", "{\"Type\"", "\"sound\"", "\"Name\"", "\"g"+ temp + "\"", "\"Dir\"", "\""+Path.GetDirectoryName(s)+ "\"", "\"Path\"", "\""+path1+ "\"", "},");
                    // }
                    Console.WriteLine(++i);
                } 
               
               
            }
            sw.Close();
            Console.ReadKey();
        }

        //FileStream fs = new FileStream("test.doc", FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
        //         StreamWriter sw = new StreamWriter(fs); // 创建写入流
        //         sw.WriteLine("bob hu"); // 写入Hello World
        //         sw.Close(); //关闭文件
    }
}

 

 

 

    原文作者:赖张殷
    原文地址: https://blog.csdn.net/qq_28221881/article/details/70574233
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞