473,748 Members | 3,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1564
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.Diagnost ics.Process.Sta rt("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.Diagnost ics.Process.Sta rt("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("myFil eName");
string ext = fi.Extension;
string fn = fi.FullName;

switch (ext)
{
case ".txt"
System.Diagnost ics.Process.Sta rt("notepad.exe ", fn);
case ".pdf"
System.Diagnost ics.Process.Sta rt("AcroRd32.ex e", 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
1856
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 conventions. 1.) You know you are at an idiot's website when there is no page title. Listen up idiot. Give every page a name using the HTML <title> element. 2.) When naming your page do not put the name of your website or your company 'after'...
4
1945
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 a file that's named 'filename.xls', but it's actually a tab delimited or comma delimited file. The possible formats that I need to identify are: HTML, tab delimited, comma delimited or Excel.
4
6096
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 implement this by MAC address. When user browser the web site I sometime can get user pc MAC and sometime no. Why this type of implementation are so not consistency? Does any
3
2601
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 user to inform him that only one instance is permit and then close the second instance after that. I am able to do this when the user run the application on his PC whit this : Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName) ...
3
3600
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 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.
1
3083
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, os, Image, ImageDraw, aggdraw def processline():
2
22896
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 = Image.open(file('c:\\image2.png')) results in
6
2493
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 locked, I want a messagebox to tell that the user has to close the file first. Is there a way to identify an exception by some kind of unique number or something like this? I don't want to identify it on the Exception Message because some users are...
6
1575
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 files from each other, reading a certain position in the stream of bytes? Or better, does anyone have a example piece of vb.net code? best regards,
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.