13,009
回編集
302行目: | 302行目: | ||
{ | { | ||
Console.WriteLine($"エラー: 移動元のディレクトリが存在しない: {sourcePath}"); | Console.WriteLine($"エラー: 移動元のディレクトリが存在しない: {sourcePath}"); | ||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== ディレクトリの名前変更 ==== | |||
以下の例では、ディレクトリの名前変更に特化したプログラムを示している。<br> | |||
これは、同じディレクトリ内でのファイル名の変更に適している。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.IO; | |||
class Program | |||
{ | |||
static void Main(string[] args) | |||
{ | |||
string dirPath = "/home/user/oldName"; | |||
string newName = "newName"; | |||
try | |||
{ | |||
string newPath = Path.Combine(Path.GetDirectoryName(dirPath), newName); | |||
if (Directory.Exists(dirPath)) | |||
{ | |||
Directory.Move(dirPath, newPath); | |||
} | |||
else | |||
{ | |||
Console.WriteLine($"エラー: 指定されたディレクトリが存在しない: {dirPath}"); | |||
} | |||
} | |||
catch (IOException e) | |||
{ | |||
Console.WriteLine($"エラー: 名前変更中にI/Oエラーが発生 {e.Message}"); | |||
} | |||
catch (UnauthorizedAccessException e) | |||
{ | |||
Console.WriteLine($"エラー: アクセス権限がない {e.Message}"); | |||
} | } | ||
} | } |