Hi, I am fiarly new to C#.
I have been trying to test the results of 2 button controls in a form.
A directory is selected with botton1_Click and a filename is selected with
button2_Click.
I want to press botton3 (the "OK" button) and test to make sure that
fileName1 and folderName1 exist or are valid. And then execute the code to
dir.GetFiles and write the filenames to the test file.
Apparently fileName1 and folderName1 are no longer available when button3 is
pressed ( button3_Click does not process the code).
The button_Click declaration is the default of the control so I am not sure
if I need something different than what I post.
I have spent a lot of time searching the archives and tutorials but don't
know enough yet to figure this one out.
Can someone tell me what would be the appropriate way to handle this?
I have looked at Delegates and property methods but need help on this.
Feel free to point out any better ways.
TIA
Bill
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.Run(new Form1());
}
public string folderName1, fileName1 ;
public void button1_Click(object sender, System.EventArgs e) //open folder
selection.
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK ) //folder selected
{
folderName1 = folderBrowserDialog1.SelectedPath;
label1.Text = folderName1 ;
}
}
public void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ; //display dialog
saveFileDialog1.OverwritePrompt = true ;
saveFileDialog1.ValidateNames = true ;
saveFileDialog1.InitialDirectory = "g:\\lsptemp" ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CheckFileExists = false ; //will not add
..ext if true.
saveFileDialog1.DereferenceLinks = false ;
saveFileDialog1.AddExtension = true ;
saveFileDialog1.DefaultExt = "TXT" ;
DialogResult result2 = saveFileDialog1.ShowDialog();
if (result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
label2.Text = fileName1 ;
}
}
public void button3_Click(object sender, System.EventArgs e)
{
if (folderName1 != null && fileName1 != null)
{
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}", file.FullName);
}
writer.Close();
MessageBox.Show("File names from: \n" +
folderName1 +
"\n "
+ "\n Written to: \n"
+ fileName1);
Application.Exit();
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
} //end main