Missing first parameter exception.

G

Guest

I am new to VsC#.
Am working on Visual Studio .NET Pro 2003.

I wrote a solution that is a form containing 2 buttons.
Button1 uses folderBrowserDialog to select a directory.
Button2 uses saveFileDialog to get a file name.

Then the file names from the directory selected are then written to the text
file.

The problem: When user dosen't select a directory and cancels out of the
folderBrowserDialog and then presses button2 and selects a text file it
thorws an exception.

What is the best way to handle this and catch the error? Here's the main code:

static void Main()
{
Application.Run(new Form1());

}
private string folderName1, fileName1 ;

private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;
}
}

private void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;

DialogResult result2 = saveFileDialog1.ShowDialog();
if( result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderName1);
StreamWriter writer = new StreamWriter(fileName1);
foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
{
writer.WriteLine(" {0}\\{1}", file.DirectoryName, file.Name);
}
writer.Close();
Application.Exit();
}
}


}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

TIA

Bill
 
A

Alex Passos

folderName1 is going to be empty which means that when you attempt to use it
you will have problems. You can check before you do anything with the
"save" dialog box if folderName1 is actually set like this:

if (folderName1.Length == 0) {
Application.MessageBox("Please use the first button");
return;
}

Alex
 
G

Guest

Hi Bill,

Have you considered setting the Enabled property to false for Button2 and
only setting that to true after Button1 is successful?
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
folderName1 = folderBrowserDialog1.SelectedPath;

// and then add something like:
if (folderName1.Length > 0)
button2.Enabled = true;

In your button2_Click you might also test the directory exists:
if (System.IO.Directory.Exists (folderName1))
and only then create the DirectoryInfo


Cheers,
Steve Goodyear
Vancouver, Canada
 
G

Guest

Thanks Alex & Steve.

So many ways, it seems, to solve probelms.

Better to look for some directions before making spagetti.

Bill
 
A

Angel J. Hernández M.

Hi there... you should verify that folderName1 != null. For example:

if( result2 == DialogResult.OK && folderName1 != null)

You can also write less code if you do something like this...

if ( saveFileDialog1.ShowDialog().Equals(DialogResult.OK) &&
folderBrowserDialog1.SelectedPath != null)

There's no need to have two members to store information about the dialogs
your're using because you can get this from their respective properties.

Regards,
 
G

Guest

Angel J. Hernández M. said:
Hi there... you should verify that folderName1 != null. For example:

if( result2 == DialogResult.OK && folderName1 != null)

You can also write less code if you do something like this...

Ah, I was thinking along these lines....

Thanks

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top