提示コードですが////
コメント部で囲ってる内部のコードで以下のErrorが発生します。原因は先にうえのディレクトリ名を変えているためその後のディレクトリパスも変更をする必要があるにも関わらず変更していないことが原因による例外なのですがこれはどう対処すればいいのでしょうか?対象方法が知りたいです。
フォルダ階層
C:\Users\yw325\Desktop\test\aaaルダー - コピー C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (10) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (11) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (12) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (2) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (3) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (4) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (5) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (6) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (7) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (8) C:\Users\yw325\Desktop\test\aaaルダー - コピー\12345 - コピー (9)
#####エラー
System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\Users\yw325\Desktop\test\aaaaaaaaルダー - コピー
cs
1using System; 2using System.IO; 3 4public class Program 5{ 6 /*################################################ 7 * 8 * 9 * 10 * 11 * 12 #################################################*/ 13 14 static List<string> dirList = new List<string>(); //ディレクトリパスリスト 15 16 public static void GetSubFolderPathList(string startPath) 17 { 18 foreach(string path in Directory.GetDirectories(startPath)) 19 { 20 if(System.IO.Directory.Exists(path) == true) 21 { 22 dirList.Add(path); 23 Console.WriteLine(path); 24 //Console.WriteLine(System.IO.Directory.GetParent(path).Name); 25// Console.WriteLine(System.IO.Path.GetFileName(path)); 26 27 // Console.WriteLine(System.IO.Path.Combine(System.IO.Directory.GetParent(path).Name,System.IO.Path.GetFileName(path))); 28 29 30 GetSubFolderPathList(path); 31 32 } 33 } 34 } 35 36 37 public static void ReWriteFolder(int position,int num) 38 { 39 40 } 41 42 43 public static int GetMostMinFolderName() 44 { 45 List<int> list = new List<int>(); 46 foreach(string path in dirList) 47 { 48 list.Add(System.IO.Path.GetFileName(path).Length); 49 } 50 51 list.Sort(); 52 53 54 return list[0]; 55 56 57 58 } 59 60 public static void Main() 61 { 62 while(true) 63 { 64 string? folderPath = null; 65 Console.Write("input rename folder path: "); 66 67 folderPath = Console.ReadLine().Trim('\"'); //フォルダーをD&D 68 69 Console.WriteLine(""); 70 71 Console.WriteLine("rewirte subfolder\n"); 72 73 GetSubFolderPathList(folderPath); //再帰的にサブフォルダーを取得 74 Console.WriteLine(""); 75 76 if (dirList.Count != 0) 77 { 78 //dirList.Add(folderPath); //ルートフォルダのパスを格納 79 80 string n; 81 bool isStart = false; 82 do 83 { 84 Console.Write("number to delete: "); 85 86 n = Console.ReadLine(); 87 88 if(n.All(char.IsDigit) == true) 89 { 90 int number = int.Parse(n); 91 92 // 書き換え結果を確認 93 foreach (string path in dirList) 94 { 95 //string parent = System.IO.Directory.GetParent(path).Name; 96 97 try 98 { 99 Console.WriteLine(System.IO.Path.GetFileName(path).Remove(0,number)); 100 } 101 catch(System.ArgumentOutOfRangeException e) 102 { 103 //指定した文字位置がフォルダ名より長いとき 104 n = (GetMostMinFolderName() - 1 ).ToString(); 105 106 Console.WriteLine(System.IO.Path.GetFileName(path).Remove(0, GetMostMinFolderName() - 1)); 107 108 } 109 110 111 //Console.WriteLine(System.IO.Path.Combine(System.IO.Directory.GetParent(path).Name, System.IO.Path.GetFileName(path))); 112 113 } 114 115 // 書き換えを実行するか確認 116 string yn; 117 do 118 { 119 Console.Write("start rewrite? [y/N]: "); 120 yn = Console.ReadLine(); 121 122 if(yn == "y") 123 { 124 isStart = true; 125 } 126 else if(yn == "N") 127 { 128 isStart = false; 129 } 130 131 } 132 while( (yn != "N") && (yn != "y") ); 133 134 } 135 136 } 137 while( (n.All(char.IsDigit) == false) || (isStart == false) ); 138 139 int num = int.Parse(n); //削除文字位置 140 141 foreach(string path in dirList) 142 { 143 // string parent = System.IO.Directory.GetParent(path).Name; 144 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 145 Console.WriteLine(System.IO.Directory.GetParent(path).ToString()); 146 string newPath = System.IO.Path.Combine(System.IO.Directory.GetParent(path).ToString(), System.IO.Path.GetFileName(path).Remove(0, num)); 147 System.IO.Directory.Move(path,newPath); 148 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 149 150 } 151 152 153 154 155 } 156 else 157 { 158 Console.WriteLine("no subfolder process cancel\n\n"); 159 } 160 161 162 dirList.Clear(); //サブフォルダーパスを初期化 163 } 164 165 166 167 168 // System.IO.Directory.Move("12345","123"); 169 170 Console.ReadLine(); 171 } 172 173}
回答1件
あなたの回答
tips
プレビュー