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
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