473,383 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Identify a file type and open the file

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

Mar 22 '06 #1
2 1541
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
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 :
System.Diagnostics.Process.Start("fileName");

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

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,

Regards,

Cerebrus.

Mar 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: clintonG | last post by:
I can't tell you how frustrated I get when going to a web developer's website and observing he or she is an idiot that has not grasped the most fundamental element of usability: page title naming...
4
by: Eric | last post by:
Hi, I need to find a way to identify between a few different file formats WITHOUT looking at the file extension. Very often our customers will name file incorrectly. For example, they'll send us...
4
by: Goh | last post by:
Hi, I would like to know how can we implement a web page that intelligent enough to unique identify that pc have been visit before without any cookies and login user require. I have try...
3
by: Michel | last post by:
Hi, I wrote an app in .Net and I whant only 1 instance of this app open for the user; the user open my app, do some works and try to open another instance of my app, I whant to show a message to...
3
by: Shilpa | last post by:
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...
1
by: Daniel Mark | last post by:
Hello all: I have following code that works well under command line, but it doesn't work after I convert it as exe application. ############### file: testPath.py import getopt, math, sys,...
2
by: h112211 | last post by:
Hi, I installed the newest available PIL (1.1.5 for Python 2.4) from their site, but cannot seem to open any files. The following from PIL import Image i =...
6
by: Pieter | last post by:
Hi, For some procedures that throws exceptions, I would like to show different messages to the user depending on what type of exception he's getting. For instance this one: when the file is...
6
by: Mobileboy36 | last post by:
I'm searching a way to identify jpg files from a digital cam Let's say we have 2 digital cameras C1 and C2 and 2 files JPG001.jpg from C1 and JPG001.jpg from C2. Is there a way to distinct the 2...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.