a8e
a8e

Reputation: 35

File.Exists -- do NOT want to create new file

I'm new to coding - so bear with me. I've done a lot of reading and can't figure this one out.

So when you run my new the app, you type in a folder name. The app goes to that folder, then will scan through 2 different log files that are in this designated folder. BUT here is where I'm getting in trouble. If the log does NOT exist - it will ask if you want to create the file that it is looking for... I do NOT want it to do that. I would just like it to go to the folder, if the file does not exist then do nothing and move on to the next line of code.

Here is the code I have so far:

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 app will display The 2013 DBTool log does not exist for this Node. - then it opens Notepad and asks me if it wants to create the file.

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

Do you want to create a new file?

I DO NOT want to create a new file, ever. Any good ways to get around this?

Upvotes: 1

Views: 611

Answers (3)

Mario S
Mario S

Reputation: 11945

While your code compiles (it's valid to just add bracers like that) it could be as simple as adding else to your if statement:

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.");
}

As your code looks now, this part will always run:

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

It's essentially the same as this code:

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

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

Upvotes: 1

Max7K
Max7K

Reputation: 151

You skipped "Else" after "if"

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.");
            }

Upvotes: 4

lindosekai
lindosekai

Reputation: 184

try this :

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

Upvotes: 1

Related Questions