gbertoli3, on 20 Sep, 2008 - 02:11 PM, said:
The problem looks like this piece of code in your tutorial:
//Set the Filename of the OpenFileDailog to nothing open.FileName = ""; //Declare filename as a String equal to the OpenFileDialog's FileName String filename = open.FileName;
You're first setting the FileName property to "" then assigning it to the filename string (so filename will always equal ""). You aren't then setting the filename after showing the dialogue. Of course, this means that when it tries to open "filename", it tries to open "", hence the exception stating "Empty path name is not legal".
Changing it to this will work:
//Declare open as a new OpenFileDailog
OpenFileDialog open = new OpenFileDialog();
//Set the Filename of the OpenFileDailog to nothing
open.FileName = "";
//Declare filename as a String equal to the OpenFileDialog's FileName
String filename = open.FileName;
//Declare filter as a String equal to our wanted OpenFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the OpenFileDialog's Filter to filter
open.Filter = filter;
//Set the title of the OpenFileDialog to Open
open.Title = "Open";
//Show the OpenFileDialog
if (open.ShowDialog(this) == DialogResult.OK)
{
// Set the filename to the dialogues value
filename = open.Filename;
//Make the txtBox's Text equal to all of the text in the OpenFileDialog's FileName(filename)
txtBox.Text = System.IO.File.ReadAllText(filename);
}
else
{
//Return
return;
}
All I've done of course is add "filename = open.Filename;" into the if before actually trying to open the file.
This post has been edited by wingot: 15 October 2008 - 01:43 AM






MultiQuote




|