Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add browse file button to Windows Form using C#

Tags:

c#

winforms

I want to select a file on the local hard disk when I click a "Browse" button.

I don't have any idea how to use the OpenFileDialog control. Can anyone help me?

like image 965
Harikasai Avatar asked Feb 15 '11 03:02

Harikasai


People also ask

How do I add a browser button to Windows Form application?

3 Answers. Show activity on this post. private void button1_Click(object sender, EventArgs e) { int size = -1; OpenFileDialog openFileDialog1 = new OpenFileDialog(); DialogResult result = openFileDialog1. ShowDialog(); // Show the dialog.

How do I add a file upload control in Windows Forms?

Press f5 key from keyword or from start button in Visual Studio. Now browse the file and click on upload button to upload the file in database. Now move to Sql Server and check file is uploaded. Also check the path folder where actual file is saving.

How do I use open file dialog in Windows form?

Forms. OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System.


2 Answers

These links explain it with examples

http://dotnetperls.com/openfiledialog

http://www.geekpedia.com/tutorial67_Using-OpenFileDialog-to-open-files.html

private void button1_Click(object sender, EventArgs e) {     int size = -1;     OpenFileDialog openFileDialog1 = new OpenFileDialog();     DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.     if (result == DialogResult.OK) // Test result.     {        string file = openFileDialog1.FileName;        try        {           string text = File.ReadAllText(file);           size = text.Length;        }        catch (IOException)        {        }     }     Console.WriteLine(size); // <-- Shows file size in debugging mode.     Console.WriteLine(result); // <-- For debugging use. } 
like image 78
Divi Avatar answered Oct 04 '22 02:10

Divi


var FD = new System.Windows.Forms.OpenFileDialog(); if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK) {     string fileToOpen = FD.FileName;      System.IO.FileInfo File = new System.IO.FileInfo(FD.FileName);      //OR      System.IO.StreamReader reader = new System.IO.StreamReader(fileToOpen);     //etc } 
like image 35
Adam Rackis Avatar answered Oct 04 '22 02:10

Adam Rackis