using System;
public class Test {
// インデント揃えて
public static void Main() {
// your code goes here
try {
// 戻り値はboolにして、二択ならboolでいいでしょ?
if (!new TestFileManage().TempFileCreate("targetPath")) {
// 下まで降りて判定いる?これでいいと思う。
return;
}
MessageBox.Show("一時ファイルが作成されました。");
/* こっから下の処理はいらない、二択で返せるでしょ?
switch (new TestFileManage().TempFileCreate("targetPath"))
{
case Ret.OK:
MessageBox.Show("一時ファイルが作成されました。");
break;
case Ret.CANCEL:
MessageBox.Show("一時ファイル作成がキャンセルされました。");
break;
case Ret.ERROR:
MessageBox.Show("一時ファイル作成に失敗しました。");
break;
defalut:
↓ 予期せぬ値って何?想定があるの?必要?(笑)
// 予期せぬ値が入ってくる場合
break;
}
*/
} catch {
MessageBox.Show("予想外のエラーが発生しました。");
// ここでエラーメッセージ込みのログ出力処理
/* こういうのいらない、ややこしい、例外処理はシンプルにして
string err = String.Empty;
if(string.IsNullOrEmpty(Common.errMsg))
{
err = Common.errMsg;
}
else
{
err = "予想外のエラーが発生しました。";
}
MessageBox.Show(err);
// ここでエラーメッセージ込みのログ出力処理
*/
}
}
//public Ret TempFileCreate(string path) {
public bool TempFileCreate(string path) {
try {
if(!Directory.Exists(path)) {
try {
Directory.CreateDirectory(path);
} catch (IOException e) {
MessageBox.Show(path + " 作成に失敗しました。");
return false;
} catch (/* アクセス不可の例外 */) {
MessageBox.Show(path + " にアクセスできませんでした。");
return false;
}
}
if(!File.Exists(path+"\\file.txt")) {
try {
File.Create(path);
} catch (IOException e) {
MessageBox.Show(path + " 作成に失敗しました。");
return false;
} catch (/* アクセス不可の例外 */) {
MessageBox.Show(path + " にアクセスできませんでした。");
return false;
}
}
return true;
/*
if(MyFile.CreateDir(path) == Ret.OK) {
return MyFile.CreateFile();
}
return Ret.CANCEL;
*/
} catch {
MessageBox.Show("予想外のエラーが発生しました。");
// ここでエラーメッセージ込みのログ出力処理
return false;
/* 一処理ずつ拾って、丸め込まないで、throwとかややこしいことしないで
Common.SetMsg("一時ファイル作成中にエラーが発生しました。");
throw;
*/
}
}
}
/* 一回しか呼ばないなら処理は全部メインクラスに入れて、クラスなんで分けるの?
public class TestFileManage
{
public Ret TempFileCreate(string path)
{
try {
if(MyFile.CreateDir(path) == Ret.OK)
{
return MyFile.CreateFile();
}
return Ret.CANCEL;
} catch {
Common.SetMsg("一時ファイル作成中にエラーが発生しました。");
throw;
}
}
}
*/
/* こういうの必要?こういうのの戻り値必要?一個一個書けばいいんじゃないの?
public class MyFile
{
public MyFile(){}
public static Ret CreateDir(string path)
{
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return Ret.OK;
}
public Ret CreateFile(string path)
{
if (Directory.Exists(path))
{
if(!File.Exists(path))
{
File.Create(path);
return Ret.OK;
} else {
return Ret.CANCEL;
}
} else {
return Ret.ERROR;
}
}
}
*/
/* ややこしいことしないでシンプルに書いてほしい
public class Common
{
public static string errMsg {set; get;}
public static void SetMsg(string msg)
{
if (String.isNullOrEmpty(errMsg))
{
errMsg = msg;
}
}
}
enum Ret : int
{
ERROR = -1, OK, CANCEL
}
*/