DAZZD88 Posted July 14, 2010 Posted July 14, 2010 Right people, I have recently attempted learning Visual C# with the help of a Sam's Learn in 24 Hours Book. I felt like I had learned just enough to muddle through creating my own simple program that, at the moment, simply lists files in a chosen directory. However, it spits out an exception when the target path is empty, which I was expecting it to do. Therefore I added an else statement (the relevant section of code is shown below) to display a messagebox informing the user of the problem. private void btnGo_Click(object sender, EventArgs e) { if (txtTargetPath != null) { [b] DirectoryInfo dirTarget = new DirectoryInfo(txtTargetPath.Text);[/b] colFiles = dirTarget.GetFiles(); foreach (FileInfo filFiles in colFiles) { txtResults.AppendText(filFiles.Name + "\r\n"); } } else MessageBox.Show("You have not selected a valid folder!"); } What have I done wrong? The line in bold in the code is the line that VC# does not like.
localzuk Posted July 14, 2010 Posted July 14, 2010 Also, when working with file systems, you should use a try { } catch() { } statement - to catch unhandled exceptions. eg. try-catch (C# Reference) 1
DAZZD88 Posted July 14, 2010 Author Posted July 14, 2010 (edited) DirectoryInfo needs a string, to try changing txtTargetPath.Text to txtTargetPath.Text.ToString You mean inside the brackets? If you did, it didn't seem to work. I think the text in the textbox should be okay because it's set to the value of strTargetPath. @localzuk will do when I get a bit more practice under my belt. I know it's not to hard to do Edited July 14, 2010 by DAZZD88
localzuk Posted July 14, 2010 Posted July 14, 2010 You mean inside the brackets? @localzuk will do when I get a bit more practice under my belt. I know it's not to hard to do Yes, inside the brackets. 'txtTargetPath.Text' outputs an object, whereas 'txtTargetPath.Text.ToString()' outputs a string.
LosOjos Posted July 14, 2010 Posted July 14, 2010 Just to add my two pence, if you search for methods on MSDN, it tells you which exceptions a method can throw and what they mean, couple that knowledge up with try-catch as localzuk said and you'll be well on your way. It's always a good idea to use try-catch when you're doing anything in your code that might throw an exception, otherwise when the program gets to the end user, rather than being able to carry on running or at least provide a description of the error, it'll just crash out to the error report screen Directory Info @ MSDN PS - in case you didn't know, when creating a new instance of a class, you're actually calling that classes constructor (thought that'd help you find the info you need on MSDN/Google) 1
DAZZD88 Posted July 14, 2010 Author Posted July 14, 2010 Here's what I have so far. The exception is now handled but is it done correctly? private void btnGo_Click(object sender, EventArgs e) { try { if (txtTargetPath != null) { DirectoryInfo dirTarget = new DirectoryInfo(txtTargetPath.Text); colFiles = dirTarget.GetFiles(); foreach (FileInfo filFiles in colFiles) { txtResults.AppendText(filFiles.Name + "\r\n"); } } } catch (Exception objException) { MessageBox.Show("An error occurred: " + objException.Message); } }
sister_annex Posted July 14, 2010 Posted July 14, 2010 Here's what I have so far. The exception is now handled but is it done correctly? private void btnGo_Click(object sender, EventArgs e) { try { if (txtTargetPath != null) { DirectoryInfo dirTarget = new DirectoryInfo(txtTargetPath.Text); colFiles = dirTarget.GetFiles(); foreach (FileInfo filFiles in colFiles) { txtResults.AppendText(filFiles.Name + "\r\n"); } } } catch (Exception objException) { MessageBox.Show("An error occurred: " + objException.Message); } } I think you still need to change where (txtTargetPath != null) to (txtTargetPath.text != null) and put an end if in
localzuk Posted July 14, 2010 Posted July 14, 2010 and put an end if in No such thing in c#. If is ended with a closing brace as he has done.
sister_annex Posted July 14, 2010 Posted July 14, 2010 Oops so it is i work in vb.net so missed that one
powdarrmonkey Posted July 14, 2010 Posted July 14, 2010 Here's what I have so far. The exception is now handled but is it done correctly? Never catch an exception you're not expecting to handle yourself. Always catch a specific exception and deal with it; if it's one you don't know how to handle, let it go up the chain to be dealt with properly by the operating system. Assess the methods you are 'try'ing and what exceptions might be thrown by them, and catch those. Example: your call to DirectoryInfo.DirectoryInfo() can throw any of ArgumentNullException, SecurityException, ArgumentException, and PathTooLongException. Though in the latter case, you've read the documentation to find the upper limit and checked your argument before passing it to DirectoryInfo.DirectoryInfo(), right? Do not just catch an exception of type Exception, except (heh) right at the top of your application where you are handling generic "something exceptional happened, deal with it gracefully and terminate" errors. 1
DAZZD88 Posted July 14, 2010 Author Posted July 14, 2010 @powdarrmonkey I get what you mean, thanks for the advice.
maxo Posted July 29, 2010 Posted July 29, 2010 If it still doesn't work, you might want to replace: if (txtTargetPath != null) with if (!String.IsNullOrEmpty(txtTargetPath.Text)) You shouldn't have to use .ToString() because txtTargetPath.Text should already be a string.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now