Windows Phone Developers

Sunday, January 25, 2009

OpenFileDialog in C# - How to Use OpenFileDialog in C# Windows Application

Common Dialog in C# (.NET)

CommonDialog was widely used to select the files in Visual Basic 6.0. .Net provides a OpenFileDialog component, which can be used for the same.

Let us have a small Windows form where the file to process is to be selected. The form contains a CommandButton to invoke the Open File dialog box and a TextBox to display the selected file.





To use the OpenFileDialog, insert the component from Toolbox to the form



When it is droped to a form, the OpenFileDialog component appears in the tray at the bottom of the Windows Forms Designer.





Now write the following code in the Button’s Click event to display the Open file dialog box

private void buttonSelectXL_Click(object sender, EventArgs e)

{

openXLFile.Filter = "Microsoft Excel Workbooks (*.xls)*.xls";

if (openXLFile.ShowDialog() == DialogResult.OK )

{

String sFileName = openXLFile.FileName;

txtExcelFile.Text = sFileName;

}

else

{

MessageBox.Show("Please select a file");

}

}







The above code filers only Excel files in the Open Dialog Box

The selected file will be displayed in the Textbox


See also OpenFileDialog in Visual Basic .Net

For CommonDialog implementation in VB6.0 refer http://vbadud.blogspot.com/2007/06/visual-basic-common-dialog.html Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

6 comments:

  1. really good .
    your blog is one of bests ;) .

    thx.

    ReplyDelete
  2. I'm trying to program a small file comparison program. I have two input files, one has one more field than the other. I need to compare each record of the text file against the other text file and extract the duplicates adding the additional field to the output file. Any suggestions? The first input file, which has the additional field can get up to 100,000 records. The other file will usually be much smaller. What is the quickest way to read through files and compare? All data is numeric. Thanks!

    ReplyDelete
  3. i'm trying to create an application that will need the user to input numeric data,and i want it to be in data grid form like a spread sheet,please how can i do that?

    ReplyDelete
  4. hi! may i know how to make the excel file that i am opening with visibility using your code?

    ReplyDelete
  5. Excel has a property Visible which can be set - Application.Visible = True

    ReplyDelete
  6. hi i have one doubt..
    i am running open dialog box in c# i want to one or more files concurrently using loops... then i want to exe.. files .. i opened any files in opendialog the exe will not running..
    i press the cancel button of open dialog box the exe will running may i know the reason ..
    any help will be greatly appreciated..
    thanks in advance here
    my coding

    TextBox txtfilelocation = new TextBox();
    int cancelcount = 0;
    OpenFileDialog ofdnew = new OpenFileDialog();
    ofdnew.Filter = "xls Files|*.xls|xlsx Files|*.xlsx|Text Files|*.txt";
    ofdnew.Title = "Select File";
    DialogResult resultnew = new DialogResult();
    //runexeDownload();
    for(int i=0;i<3;i++)
    {
    resultnew = ofdnew.ShowDialog();
    ofdnew.Filter = "All Files|*.*";
    ofdnew.FilterIndex = 2;
    ofdnew.RestoreDirectory = true;
    ofdnew.Reset();
    ofdnew.Dispose();
    ofdnew.FileName = "F:\\Documents and Settings\\Origin\\My Documents\\Visual Studio 2008\\Projects\\Test1\\Test1\\bin\\Debug\\Download.txt";
    runexeDownload();
    ////int dialog = 0;
    if (resultnew == DialogResult.OK)
    {
    txtfilelocation.Text = ofdnew.FileName;
    }
    cancelcount = 0;
    while (resultnew == DialogResult.Cancel && cancelcount != 2)
    {
    cancelcount++;
    resultnew = ofdnew.ShowDialog();
    if (resultnew == DialogResult.OK)
    {
    txtfilelocation.Text = ofdnew.FileName;
    }
    }
    if (cancelcount == 2 && resultnew == DialogResult.Cancel)
    {
    ofdnew.Dispose();
    // int filecount = 0;
    }


    }
    //runexeDownload();
    }

    // reply to my mail id bharathiit09@gmail.com thanks in advance..


    **** i think little bit diff to understand the problem doubts on my questions free to mail @ bharathiit09@gmail.com //

    ReplyDelete