最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 文件操作(移動(dòng),復(fù)制,重命名)

 更新時(shí)間:2020年12月21日 09:27:29   作者:yangyang  
這篇文章主要介紹了c# 如何對(duì)文件操作(移動(dòng),復(fù)制,重命名),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

文件移動(dòng)

public static void MoveFolder(string sourcePath, string destPath)
    {
      if (Directory.Exists(sourcePath))
      {
        if (!Directory.Exists(destPath))
        {
          //目標(biāo)目錄不存在則創(chuàng)建 
          try
          {
            Directory.CreateDirectory(destPath);
          }
          catch (Exception ex)
          {
            throw new Exception("創(chuàng)建目標(biāo)目錄失?。? + ex.Message);
          }
        }
        //獲得源文件下所有文件 
        List<string> files = new List<string>(Directory.GetFiles(sourcePath));
        files.ForEach(c =>
        {
          string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
          //覆蓋模式 
          if (File.Exists(destFile))
          {
            File.Delete(destFile);
          }
          File.Move(c, destFile);
        });
        //獲得源文件下所有目錄文件 
        List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));

        folders.ForEach(c =>
        {
          string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
          //Directory.Move必須要在同一個(gè)根目錄下移動(dòng)才有效,不能在不同卷中移動(dòng)。 
          //Directory.Move(c, destDir); 

          //采用遞歸的方法實(shí)現(xiàn) 
          MoveFolder(c, destDir);
        });
      }
      else
      {
    

文件復(fù)制

public static void CopyFilefolder(string sourceFilePath, string targetFilePath)
    {
      //獲取源文件夾中的所有非目錄文件
      string[] files = Directory.GetFiles(sourceFilePath);
      string fileName;
      string destFile;
      //如果目標(biāo)文件夾不存在,則新建目標(biāo)文件夾
      if (!Directory.Exists(targetFilePath))
      {
        Directory.CreateDirectory(targetFilePath);
      }
      //將獲取到的文件一個(gè)一個(gè)拷貝到目標(biāo)文件夾中 
      foreach (string s in files)
      {
        fileName = Path.GetFileName(s);
        destFile = Path.Combine(targetFilePath, fileName);
        File.Copy(s, destFile, true);
      }
      //上面一段在MSDN上可以看到源碼 

      //獲取并存儲(chǔ)源文件夾中的文件夾名
      string[] filefolders = Directory.GetFiles(sourceFilePath);
      //創(chuàng)建Directoryinfo實(shí)例 
      DirectoryInfo dirinfo = new DirectoryInfo(sourceFilePath);
      //獲取得源文件夾下的所有子文件夾名
      DirectoryInfo[] subFileFolder = dirinfo.GetDirectories();
      for (int j = 0; j < subFileFolder.Length; j++)
      {
        //獲取所有子文件夾名 
        string subSourcePath = sourceFilePath + "\\" + subFileFolder[j].ToString();
        string subTargetPath = targetFilePath + "\\" + subFileFolder[j].ToString();
        //把得到的子文件夾當(dāng)成新的源文件夾,遞歸調(diào)用CopyFilefolder
        CopyFilefolder(subSourcePath, subTargetPath);
      }
    }

文件重命名

public ExecutionResult FileRename(string sourceFile, string destinationPath, string destinationFileName)
    {
      ExecutionResult result;
      FileInfo tempFileInfo;
      FileInfo tempBakFileInfo;
      DirectoryInfo tempDirectoryInfo;

      result = new ExecutionResult();
      tempFileInfo = new FileInfo(sourceFile);
      tempDirectoryInfo = new DirectoryInfo(destinationPath);
      tempBakFileInfo = new FileInfo(destinationPath + "\\" + destinationFileName);
      try
      {
        if (!tempDirectoryInfo.Exists)
          tempDirectoryInfo.Create();
        if (tempBakFileInfo.Exists)
          tempBakFileInfo.Delete();
        //move file to bak
        tempFileInfo.MoveTo(destinationPath + "\\" + destinationFileName);

        result.Status = true;
        result.Message = "Rename file OK";
        result.Anything = "OK";
      }
      catch (Exception ex)
      {
        result.Status = false;
        result.Anything = "Mail";
        result.Message = ex.Message;
        if (mesLog.IsErrorEnabled)
        {
          mesLog.Error(MethodBase.GetCurrentMethod().Name, "Rename file error. Msg :" + ex.Message);
          mesLog.Error(ex.StackTrace);
        }
      }

      return result;
    }

以上就是c# 文件操作(移動(dòng),復(fù)制,重命名)的詳細(xì)內(nèi)容,更多關(guān)于c# 文件操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 深入分析C# Task

    深入分析C# Task

    這篇文章主要介紹了C# Task的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí)C# Task的相關(guān)知識(shí),感興趣的朋友可以了解下
    2020-08-08
  • C#中判斷、驗(yàn)證字符串是否為日期格式的實(shí)現(xiàn)代碼

    C#中判斷、驗(yàn)證字符串是否為日期格式的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C#中判斷、驗(yàn)證字符串是否為日期格式的實(shí)現(xiàn)代碼,使用DateTime類中自帶的兩個(gè)方法實(shí)現(xiàn),需要的朋友可以參考下
    2014-08-08
  • 淺析C#封裝GRPC類庫(kù)及調(diào)用簡(jiǎn)單實(shí)例

    淺析C#封裝GRPC類庫(kù)及調(diào)用簡(jiǎn)單實(shí)例

    這篇文章主要為大家詳細(xì)介紹了C#中封裝GRPC類庫(kù)及調(diào)用簡(jiǎn)單實(shí)例的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • C# WinForm捕獲未處理的異常實(shí)例解析

    C# WinForm捕獲未處理的異常實(shí)例解析

    這篇文章主要介紹了C# WinForm捕獲未處理的異常,包括了常見(jiàn)的未捕獲的異常、UI線程異常、非UI線程異常等,非常實(shí)用,需要的朋友可以參考下
    2014-09-09
  • WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法

    WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法

    這篇文章主要介紹了WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了WinForm實(shí)現(xiàn)comboBox控件數(shù)據(jù)綁定的常用方法與相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • WinForm中實(shí)現(xiàn)picturebox自適應(yīng)圖片大小的方法

    WinForm中實(shí)現(xiàn)picturebox自適應(yīng)圖片大小的方法

    這篇文章主要介紹了WinForm中實(shí)現(xiàn)picturebox自適應(yīng)圖片大小的方法,涉及pictureBox控件相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下
    2017-05-05
  • WinForm中Application.Idle方法詳解

    WinForm中Application.Idle方法詳解

    本文詳細(xì)講解了WinForm中的Application.Idle方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能

    C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C#中隱式運(yùn)行CMD命令行窗口的方法

    C#中隱式運(yùn)行CMD命令行窗口的方法

    下面介紹一種常用的在C#程序中調(diào)用CMD.exe程序,并且不顯示命令行窗口界面,來(lái)完成CMD中各種功能的簡(jiǎn)單方法。
    2011-04-04
  • Unity讀取Excel文件轉(zhuǎn)換XML格式文件

    Unity讀取Excel文件轉(zhuǎn)換XML格式文件

    這篇文章主要為大家詳細(xì)介紹了Unity讀取Excel文件轉(zhuǎn)換XML格式文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06

最新評(píng)論

临沧市| 茶陵县| 博湖县| 铅山县| 久治县| 龙泉市| 夏河县| 海晏县| 颍上县| 衡阳县| 宁蒗| 全椒县| 太谷县| 翼城县| 潜山县| 米脂县| 古田县| 上思县| 常熟市| 临潭县| 宽城| 重庆市| 慈溪市| 遂溪县| 沅江市| 龙游县| 碌曲县| 册亨县| 南通市| 乡城县| 肥东县| 孟津县| 龙南县| 邛崃市| 大新县| 巴中市| 桦川县| 广丰县| 淳化县| 浙江省| 新民市|