fork(1) download
  1. using System;
  2.  
  3. public class Test {
  4. // インデント揃えて
  5. public static void Main() {
  6. // your code goes here
  7. try {
  8. // 戻り値はboolにして、二択ならboolでいいでしょ?
  9. if (!new TestFileManage().TempFileCreate("targetPath")) {
  10. // 下まで降りて判定いる?これでいいと思う。
  11. return;
  12. }
  13. MessageBox.Show("一時ファイルが作成されました。");
  14.  
  15. /* こっから下の処理はいらない、二択で返せるでしょ?
  16. switch (new TestFileManage().TempFileCreate("targetPath"))
  17. {
  18. case Ret.OK:
  19. MessageBox.Show("一時ファイルが作成されました。");
  20. break;
  21. case Ret.CANCEL:
  22. MessageBox.Show("一時ファイル作成がキャンセルされました。");
  23. break;
  24. case Ret.ERROR:
  25. MessageBox.Show("一時ファイル作成に失敗しました。");
  26. break;
  27. defalut:
  28. ↓ 予期せぬ値って何?想定があるの?必要?(笑)
  29. // 予期せぬ値が入ってくる場合
  30. break;
  31. }
  32. */
  33. } catch {
  34. MessageBox.Show("予想外のエラーが発生しました。");
  35. // ここでエラーメッセージ込みのログ出力処理
  36.  
  37. /* こういうのいらない、ややこしい、例外処理はシンプルにして
  38. string err = String.Empty;
  39. if(string.IsNullOrEmpty(Common.errMsg))
  40. {
  41. err = Common.errMsg;
  42. }
  43. else
  44. {
  45. err = "予想外のエラーが発生しました。";
  46. }
  47. MessageBox.Show(err);
  48. // ここでエラーメッセージ込みのログ出力処理
  49. */
  50. }
  51. }
  52.  
  53. //public Ret TempFileCreate(string path) {
  54. public bool TempFileCreate(string path) {
  55. try {
  56. if(!Directory.Exists(path)) {
  57. try {
  58. Directory.CreateDirectory(path);
  59. } catch (IOException e) {
  60. MessageBox.Show(path + " 作成に失敗しました。");
  61. return false;
  62. } catch (/* アクセス不可の例外 */) {
  63. MessageBox.Show(path + " にアクセスできませんでした。");
  64. return false;
  65. }
  66. }
  67. if(!File.Exists(path+"\\file.txt")) {
  68. try {
  69. File.Create(path);
  70. } catch (IOException e) {
  71. MessageBox.Show(path + " 作成に失敗しました。");
  72. return false;
  73. } catch (/* アクセス不可の例外 */) {
  74. MessageBox.Show(path + " にアクセスできませんでした。");
  75. return false;
  76. }
  77. }
  78. return true;
  79. /*
  80. if(MyFile.CreateDir(path) == Ret.OK) {
  81. return MyFile.CreateFile();
  82. }
  83. return Ret.CANCEL;
  84. */
  85. } catch {
  86. MessageBox.Show("予想外のエラーが発生しました。");
  87. // ここでエラーメッセージ込みのログ出力処理
  88. return false;
  89. /* 一処理ずつ拾って、丸め込まないで、throwとかややこしいことしないで
  90. Common.SetMsg("一時ファイル作成中にエラーが発生しました。");
  91. throw;
  92. */
  93. }
  94. }
  95. }
  96.  
  97. /* 一回しか呼ばないなら処理は全部メインクラスに入れて、クラスなんで分けるの?
  98. public class TestFileManage
  99. {
  100. public Ret TempFileCreate(string path)
  101. {
  102. try {
  103. if(MyFile.CreateDir(path) == Ret.OK)
  104. {
  105. return MyFile.CreateFile();
  106. }
  107. return Ret.CANCEL;
  108. } catch {
  109. Common.SetMsg("一時ファイル作成中にエラーが発生しました。");
  110. throw;
  111. }
  112. }
  113. }
  114. */
  115.  
  116. /* こういうの必要?こういうのの戻り値必要?一個一個書けばいいんじゃないの?
  117. public class MyFile
  118. {
  119. public MyFile(){}
  120.  
  121. public static Ret CreateDir(string path)
  122. {
  123. if(!Directory.Exists(path))
  124. {
  125. Directory.CreateDirectory(path);
  126. }
  127. return Ret.OK;
  128. }
  129.  
  130. public Ret CreateFile(string path)
  131. {
  132. if (Directory.Exists(path))
  133. {
  134. if(!File.Exists(path))
  135. {
  136. File.Create(path);
  137. return Ret.OK;
  138. } else {
  139. return Ret.CANCEL;
  140. }
  141. } else {
  142. return Ret.ERROR;
  143. }
  144. }
  145.  
  146. }
  147. */
  148.  
  149. /* ややこしいことしないでシンプルに書いてほしい
  150. public class Common
  151. {
  152. public static string errMsg {set; get;}
  153.  
  154. public static void SetMsg(string msg)
  155. {
  156. if (String.isNullOrEmpty(errMsg))
  157. {
  158. errMsg = msg;
  159. }
  160. }
  161. }
  162.  
  163. enum Ret : int
  164. {
  165. ERROR = -1, OK, CANCEL
  166. }
  167. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(62,29): error CS1015: A type that derives from `System.Exception', `object', or `string' expected
prog.cs(73,29): error CS1015: A type that derives from `System.Exception', `object', or `string' expected
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty