<%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/tutorials/FileOpen-Dialog.aspx.cs" Inherits="FileOpenDialogTutorial" %> OpenFile Dialog API

Members Library
  C# Video Tutorials
Visual Studio 2008
Intro to VS 2008 Environment
My First Program
Creating Mulit-Project Solutions
Working with Strings
Contains
Length
Stringbuilder
Substring
Trim
Upper Case / Lower Case
IndexOf
Concatenation
Split
String Compare
Manipulating Text Files
Validating User Input
Creating Custom Controls
Using DateTime
Using the Windows API
.NET Forms

In Production
Automate Word with C#
Automate Excel with C#
Create PDF on the fly
Win API Calls
Using the System.Drawing Object
Get image from SQL Server
Upload image to SQL2005 database
Connecting to SQL Server

Coming this week!
Access CD Drive via API
Connecting to SQL Server 2005
Automating Excel with C#
A Custom Rounded Panel Control


JET
TECHNICAL.COM

jet tech
C Sharp Video Tutorials Online
all skill levels
member news
videos
sample code
C# On Demand!

How to use the OpenFile Dialog API with C Sharp

The OpenFileDialog object interacts with the Computer’s API (Application Programming Interface) to present available files to the user and retrieves the user’s file selection back to the program. This object is part of the System.Windows.Forms library so no additional using reference will be needed.

This example uses a button click method.

private void btnOpenTextFile_Click(object sender, EventArgs e)
{
//First, declare a variable to hold the user’s file selection.
private void String input = string.Empty;

//Create a new instance of the OpenFileDialog because it's an object.
private voidOpenFileDialog dialog = new OpenFileDialog();

//Now set the file type
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

//Set the starting directory and the title.
dialog.InitialDirectory = "C:"; dialog.Title = "Select a text file";

//Present to the user.
if (dialog.ShowDialog() == DialogResult.OK)
     strFileName = dialog.FileName;
if (strFileName == String.Empty)
     return;//user didn't select a file
}


If the user selected a file, then the DialogResult property value will be “OK” and we will also have the file name and path of that file. Now we can use the StreamWriter and StreamReader to read/write to the text file.

Let us hear your comments, send us an email.
Members Library
  Source Code Downloads
DateTime Functions
Custom Controls
Working with Strings
Working with Text Files
Validating User Input

Free Downloads
Visual Studio Express

Crystal Reports 2008
crystal reports

--MEMBER LOGIN--
email address: password:     







business broker businesses for sale