By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

Identify a file type and open the file

P: n/a
Hi All,

I want to write C# code to identify a file type and open the file in
the associated editor.
For example, text files should be identified and opened in notepad,
html should be opened in internet explorer/netscape/mozilla.
At design time, I do not know if internet explorer/netscape is
installed on the client machine. The C# code should identify that and
Please help.

Mar 22 '06 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Shilpa wrote:
Hi All,

I want to write C# code to identify a file type and open the file in
the associated editor.

Can you just look at the file extension?
Mar 22 '06 #2

P: n/a
Hi Shilpa,

There are a few ways to do this. I will describe three of them :

1. Files usually open in the associated editor by themself. So, you can
use :
System.Diagnostics.Process.Start("myProgram", "arguments");

Here myProgram is the path to the executable that opens the file, such
as Notepad.
Arguments is the FileName.

2. To generalize the above process, you can simply use :

This will automatically open any file in the associated program. If an
HTML file is selected, and multiple browsers are installed, it will
open the file in the System Default browser, be it IE, or Mozilla or

3. To account for cases where unrecognized file types may be passed as
parameters, you could a Select Case structure (Switch structure in C#),
wherein you could check the File Extenstion of each passed file and
make decisions accordingly :

FileInfo fi = new FileInfo("myFileName");
string ext = fi.Extension;
string fn = fi.FullName;

switch (ext)
case ".txt"
System.Diagnostics.Process.Start("notepad.exe", fn);
case ".pdf"
System.Diagnostics.Process.Start("AcroRd32.exe", fn);

Typed here, so watch for syntax...

Hope this helps,



Mar 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.