1

私はコーディングに不慣れです-だから私に耐えてください。私はたくさんの読書をしました、そしてこれを理解することができません。

したがって、新しいアプリを実行するときは、フォルダー名を入力します。アプリはそのフォルダーに移動し、この指定されたフォルダーにある2つの異なるログファイルをスキャンします。しかし、ここで私は困っています。ログが存在しない場合-探しているファイルを作成するかどうかを尋ねられます...私はそれをしたくありません。フォルダに移動したいのですが、ファイルが存在しない場合は何もせずに次のコード行に進みます。

これが私がこれまでに持っているコードです:

private void btnGo_Click(object sender, EventArgs e)
{
    //get node id from user and build path to logs
    string nodeID = txtNodeID.Text;
    string serverPath = @"\\Jhexas02.phiext.com\rwdata\node\";
    string fullPath = serverPath + nodeID;
    string dbtoolPath = fullPath + "\\DBTool_2013.log";
    string msgErrorPath = fullPath + "\\MsgError.log";

    //check if logs exist
    if (File.Exists(dbtoolPath) == true)
    {
        System.Diagnostics.Process.Start("notepad.exe", dbtoolPath);
    }
    {
        MessageBox.Show("The 2013 DBTool log does not exist for this Node.");
    }

アプリが表示されますThe 2013 DBTool log does not exist for this Node.-次にメモ帳が開き、ファイルを作成するかどうかを尋ねられます。

Cannot find the \\Jhexas02.phiext.com\rwdata\node\R2379495\DBTool_2013.log file.

Do you want to create a new file?

新しいファイルを作成したくありません。これを回避するための良い方法はありますか?

4

3 に答える 3

4

「if」の後に「Else」をスキップしました

if (File.Exists(dbtoolPath) == true)
            {
                System.Diagnostics.Process.Start("notepad.exe", dbtoolPath);
            }
            else
            {
                MessageBox.Show("The 2013 DBTool log does not exist for this Node.");
            }
于 2013-02-04T22:09:39.590 に答える
1

コードをコンパイルしている間(そのようなブレーサーを追加するだけで有効です) 、ステートメントに追加elseするのと同じくらい簡単です。if

if (File.Exists(dbtoolPath) == true) // This line could be changed to: if (File.Exists(dbtoolPath))
{
    System.Diagnostics.Process.Start("notepad.exe", dbtoolPath);
}
else // Add this line.
{
    MessageBox.Show("The 2013 DBTool log does not exist for this Node.");
}

コードが今のように見えるように、この部分は常に実行されます:

{
    MessageBox.Show("The 2013 DBTool log does not exist for this Node.");
}

これは基本的に次のコードと同じです。

if (File.Exists(dbtoolPath) == true)
{
    System.Diagnostics.Process.Start("notepad.exe", dbtoolPath);
}

MessageBox.Show("The 2013 DBTool log does not exist for this Node.");
于 2013-02-04T22:08:25.860 に答える
1

これを試して :

if (File.Exists(dbtoolPath))
    {
        System.Diagnostics.Process.Start("notepad.exe", dbtoolPath);
    }
else {
        MessageBox.Show("The 2013 DBTool log does not exist for this Node.");
    }
于 2013-02-04T22:09:21.003 に答える