Identify a file type and open a file

  • Thread starter Thread starter Shilpa
  • Start date Start date
S

Shilpa

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
open.

Please help.

Regards,
Shilpa
 
Shilpa said:
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
open.

What you want to do is not necessary. Simply set the file name to the
Command property of the ProcessInfo class and then execute
ProcessInfo.Start. The class will figure out the appropriate editor to
start. However, if the file has no association, you'll get a
Win32Exception which you should catch and decide what to do next.

Regards
 
Shilpa said:
Hey Frank,

Thanks a lot.
Could you please send a code sample.

Regards,
Shilpa

System.Diagnostics.Process prc = new
System.Diagnostics.Process();

try
{
prc.StartInfo.FileName = "myfile.doc";
prc.Start();
}
catch (System.ComponentModel.Win32Exception ex)
{


}
catch (Exception ex)
{


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top