views:

44

answers:

2

Hi, i am working on form . i want small window to pop up when i click button and to will select XML file of my chois from various folder.

I guess, this OPenfileDialog will help me.

check the code.

private void button3_Click(object sender, EventArgs e)
        {
           /
            OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = " XML Files|*.xml";

            openFileDialog1.InitialDirectory = @"D:\";



            if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(filed.FileName.ToString());
            }

       }

I tried using following code but when i click on the button there window doesn't pop up. i am not geting what mistake i have made.

What is the problem with that?

Thanx

A: 

You cant open file fialog in console app.

You say I have button, so this must be Win app, use

openFileDialog1.ShowDialog(); is missing

private void button3_Click(object sender, EventArgs e)
        {
           OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = " XML Files|*.xml";

            openFileDialog1.InitialDirectory = @"D:\";

            openFileDialog1.ShowDialog();

            // Get file name and use OpenFileDialog1.FileName or something like that

       }
Serkan Hekimoglu
Thanx....Guys..i think its worikng now....
Sheela
If you find your answer, please mark as accepted.
Serkan Hekimoglu
+1  A: 

You cant just open the file dialog from a console app. You will have to workaround it with some setting to single thread apartment (STA).

[STAThread]
static void Main(string[] args)
{
            MessageBox.Show("Test");
}

--EDIT--

Following works on click event:

OpenFileDialog f = new OpenFileDialog();
f.Filter = "XML Files|*.xml";
f.InitialDirectory = "D:\\"; 
if(f.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(f.FileName);  
}
KMan
Sorry. its form buddy !! i m st8ll no geting it ....code ...............private void button3_Click(object sender, EventArgs e) { openFileDialog1.Filter = " XML Files|*.xml"; openFileDialog1.InitialDirectory = @"D:\"; path = openFileDialog1.FileName.ToString(); openFileDialog1.ShowDialog(); }
Sheela
@Sheela: Please see my edit in response to your comment.
KMan