Created
July 31, 2017 11:47
-
-
Save akira345/6596daeafa53b78349ca9b52c9f0838e to your computer and use it in GitHub Desktop.
C#で一時ディレクトリ作成スニペット
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //一時ディレクトリ作成 | |
| 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