iT邦幫忙

0

C# 資料夾路徑問題

資料夾結構 C:\V_M{Code}\Temp\2021\A01
我需要到A01這個子資料夾中去做新增檔案或壓縮等等動作
但我不會知道這層資料夾的名稱{Code}只知道他是10碼數字
請問 路徑該怎麼處理?

Homura iT邦高手 1 級 ‧ 2021-09-27 19:33:15 檢舉
不太懂意思
你是說code會動態變化嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
小魚
iT邦大師 1 級 ‧ 2021-09-27 19:41:11

列出所有資料夾, 找出符合條件的.

3
孤獨一隻雞
iT邦研究生 5 級 ‧ 2021-09-27 22:48:03

用GetFiles去撈出所有資料夾
FileInfo[] fis = di.GetFiles();
用foreach迴圈去拆解fis的到 FileInfo fi,
然後用 if (fi.FullName.IndexOf("V_M") >= 0)
就會是你要的資料夾了

1
Yaowen
iT邦研究生 4 級 ‧ 2021-09-28 09:49:28
  String LogPath = " C:\V_M{Code}\Temp\2021\A01";

  Log(LogPath);

  private static void Log(string logPath)
  {
            
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(logPath);
            fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
            System.IO.File.SetAttributes(logPath, System.IO.FileAttributes.Normal);
            //判斷資料夾是否還存在
            if (Directory.Exists(logPath))
            {
                foreach (string f in Directory.GetFileSystemEntries(logPath))
                {
                    if (File.Exists(f))
                    {
                        //做你想做的事情
                    }
 
                }
            }
  }
0
JC
iT邦新手 4 級 ‧ 2021-09-28 11:22:59

可以參考使用MSDN中的 DirectoryInfo.GetFiles() 方法

https://docs.microsoft.com/zh-tw/dotnet/api/system.io.directoryinfo.getfiles?view=net-5.0

先挖出C:\V_M{Code}的全部目錄,再將後面的\Temp\2021\A01當作字串帶入找到想要的子目錄後,接續後面想要做的動作

一點想法給您參考

我要發表回答

立即登入回答