Skip to content

Instantly share code, notes, and snippets.

@akira345
Created July 31, 2017 11:47
Show Gist options
  • Save akira345/6596daeafa53b78349ca9b52c9f0838e to your computer and use it in GitHub Desktop.
Save akira345/6596daeafa53b78349ca9b52c9f0838e to your computer and use it in GitHub Desktop.
C#で一時ディレクトリ作成スニペット
//一時ディレクトリ作成
string temp_path = Path.GetTempPath();
string temp_dir_name = Path.GetRandomFileName();
string temp_dir = Path.Combine(temp_path, temp_dir_name);
try
{
if (Directory.Exists(Path.Combine(temp_path, temp_dir_name)))
{
Directory.Delete(temp_dir, true);
}
Directory.CreateDirectory(temp_dir);
}
catch (System.UnauthorizedAccessException)
{
MessageBox.Show("アクセス権がありません。" + Environment.NewLine + "Access to the directory " + temp_dir + " is not permitted", "エラー",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
MessageBox.Show(temp_dir);
//ゴミ掃除
try
{
if (Directory.Exists(Path.Combine(temp_path, temp_dir_name)))
{
Directory.Delete(temp_dir, true);
}
}
catch (System.UnauthorizedAccessException)
{
MessageBox.Show("アクセス権がありません。" + Environment.NewLine + "Access to the directory " + temp_dir + " is not permitted", "エラー",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment