Reputation: 543
I have a text box. When I drag a file into it and click a button the file from text box is moved to another folder. But if I forget to drag a file to the text box and click a button, the program throws an Argument Exception that sourceFileName isnt't present.
private void button_Click(object sender, EventArgs e)
{
// here occurs Argument Exception Because there isn't any TextBox.Text
File.Copy(TextBox.Text, "C:/");
if (String.IsNullOrEmpty(TextBox.Text))
{
MessageBox.Show("Please drag a file to the Tex Box!");
}
}
How can I catch that Argument Exception of a missing Source File?
Upvotes: 0
Views: 286
Reputation: 8902
if (File.Exists(TextBox.Text.Trim()))
{
File.Copy(TextBox.Text, "C:/");
}
else
{
MessageBox.Show("Please drag a file to the Tex Box!");
return;
}
Upvotes: 0
Reputation: 219057
But if I forget to drag a file to the text box
Then you need to do some error checking in your code. If it's possible in the UI to click the button without specifying a file, then your code can't assume that the file name will be present. (Currently it does.)
Try checking the condition before performing the file operations:
if (!string.IsNullOrEmpty(TextBox.Text))
File.Copy(TextBox.Text, "C:/");
Maybe even take it a step further to check if the value actually is a valid file:
if (! string.IsNullOrEmpty(TextBox.Text))
if (File.Exists(TextBox.Text))
File.Copy(TextBox.Text, "C:/");
Add some else
conditions to show appropriate messages to the user. Conversely, you can disable the button in the UI until the text box has a value. Or, well, you could take both approaches.
(As an aside, TextBox
isn't a particularly good name for a variable. That's the name of an actual class, and probably the same class as the variable itself. It's best to differentiate between the class name and the variable instance name, especially if you're ever going to use static methods on that class.)
Upvotes: 2
Reputation: 419
I tend to use File.Exists to see if the file is there beforehand I like to use exceptions for exceptional cases, but that's personal preference
Upvotes: 1
Reputation: 25370
why do you do the check after?
if (String.IsNullOrEmpty(TextBox.Text))
MessageBox.Show("Please drag a file to the Tex Box!");
else
File.Copy(TextBox.Text, "C:/");
you only want to perform the action if it's valid. Really you probably need a try/catch around the whole thing as multiple errors could happen
Upvotes: 1
Reputation: 6304
Move your String.IsNullOrEmpty
statement before the other statement.
private void button_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(TextBox.Text))
MessageBox.Show("Please drag a file to the Tex Box!");
else
File.Copy(TextBox.Text, "C:/");
}
Upvotes: 1
Reputation: 223402
Check for string.IsNullOrEmpty
before File.Copy
and if the TextBox
is empty then return
from the event.
private void button_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(TextBox.Text))
{
MessageBox.Show("Please drag a file to the Tex Box!");
return; // return from the event
}
File.Copy(TextBox.Text, "C:/");
}
Also its better if you use string.IsNullOrWhiteSpace
, since it will check for null
, string.Empty
and white spaces (Only if you are using .Net framework 4.0 or higher)
Upvotes: 3