0

I need to look if a path is available, but only at the runtime. So if my program has checked that the file doesn't exist, it sets the invalidPath bool to "true", so that it doesn't jump in the "File.Exists..." query again, as long the program runs.

The problem is: if I do it in that way like listed below, I see no possibility to avoid code redundancy. I need to show a message to inform the user about the missing file, but it isn't very elegant to use the same line(s) of code for both "if" cases.

private bool invalidPath = false

if (!invalidPath)
    {
      if (File.Exists(temp))
      {
          //do code            
      }
      else
      {
          Show.MessageBox("no file found") 
          invalidPath = true   
      }
    }
    else
    {
       Show.Messagebox("no file found")   /*<---thats redundant*/   
    }

I hope someone can give me an idea.

3 Answers 3

2

Try this:

            bool invalidPath = false;
            bool fileExistsflag = false;               
            if (!invalidPath)
            {
                if (File.Exists(temp))
                {
                    //do code  
                    fileExistsflag = true;
                }
                else
                {
                    invalidPath = true;
                }
            }
            if (!fileExistsflag || invalidPath)
            { 
                MessageBox.Show("no file found");
            }
Sign up to request clarification or add additional context in comments.

2 Comments

Seems I have to make one another bool. Thanks I'm pleased with this solution.
Great. :) Please mark this as answer, if you find this as correct solution. Thanks
1

is there a reason why you don't want to combine the boolean statements in a single if block?

i.e.

private bool invalidPath = false

if (!invalidPath && File.Exists(temp)) {
    //do code            
}
else {
    Show.MessageBox("no file found") 
    invalidPath = true   
}

3 Comments

If I combine those two statements, the program/method would search for the file everytime when it's called (either by wrong and right paths) and that's what I try to prevent, because searching for the file all the time would cost unnecessary runtime.
@Klunky actually, if first condition fails, the following conditions will not even be evaluated. this is called 'short-circuit'. you can read more here: msdn.microsoft.com/en-us/library/2a723cdk(v=vs.71).aspx
Oh, I didn't know (I do not have that much experience in C#/.NET). That is pretty useful. It reduces one if statement and do not require further variables. That's ideal for me, thanks!
0

You cannot use a single boolean to hold both Information (Path checked and File exists). Thus you have to use a second boolean.

The pseudo-code would look like this:

bool valid = false;
bool checked = false;

if(!checked)
{
  valid = File.Exists("bla");
  checked = true;
}

if(!valid)
{
  MessageBox("Path does not exist");
}

However, your strategy might have issues. For example a user might remove or rename the path while your program is running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.