473,418 Members | 2,138 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,418 software developers and data experts.

Opening a File using "File.Open"

Greetings,

I have a "newbie" question in relation to opening files from C#. I have
a Windows form where I allow the user to type in a file extension in a
text box (e.g., "xls"). I then take that extension and use that as my
filter criteria for the File Open dialog.

Once the user selects a file with that extension (from the File Open
dialog), I simply want to open that file (whether it is an .xls file,
.txt file, etc.). I am attempting to open the file using the
"System.IO.File.Open" command, but nothing seems to happen. Any ideas?

Thanks in advance!

************************************************** ******

private void btnOpenFile_Click(object sender, System.EventArgs e)
{
string strExt = this.txtFileExtension.Text;
strExt = strExt + " Files|*." + strExt;
this.openFileDialog1.InitialDirectory = @"C:\";
this.openFileDialog1.Filter = strExt;
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
txtSource.Text = openFileDialog1.FileName;
else
txtSource.Text = "";

System.IO.File.Open(txtSource.Text, FileMode.Open,
FileAccess.Read, FileShare.None);
}


*** Sent via Developersdex http://www.developersdex.com ***
Apr 25 '06 #1
2 4378
OutdoorGuy <Ou********@fishing.com> wrote:
I have a "newbie" question in relation to opening files from C#. I have
a Windows form where I allow the user to type in a file extension in a
text box (e.g., "xls"). I then take that extension and use that as my
filter criteria for the File Open dialog.

Once the user selects a file with that extension (from the File Open
dialog), I simply want to open that file (whether it is an .xls file,
txt file, etc.). I am attempting to open the file using the
"System.IO.File.Open" command, but nothing seems to happen. Any ideas?


I suspect you think that File.Open does something completely different
to what it actually does.

I suspect you want to "launch" that file, whereas File.Open opens the
file for reading/writing from your program.

If you do want to launch whatever program is associated with that file,
you should look into System.Diagnostics.Process - although there may
well be subtly different ways of launching the associated program
depending on your platform. (It may be simple - I don't know, I haven't
had to do it.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 25 '06 #2
OutdoorGuy,

As Jon indicated, it "sounds" like you want to execute the chosen file based
on its Associated Application (Excel, Word, Notepad, etc.) rather than "open
the file".

You can try System.DiagnosticsProcess.Start(fullfilename); and see what
happens. It will run the file using the default file association for the
extension from the Registry.
For example, if you wanted to bring up google.com:

Process.Start("Http://www.google.com");

Peter

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"OutdoorGuy" wrote:
Greetings,

I have a "newbie" question in relation to opening files from C#. I have
a Windows form where I allow the user to type in a file extension in a
text box (e.g., "xls"). I then take that extension and use that as my
filter criteria for the File Open dialog.

Once the user selects a file with that extension (from the File Open
dialog), I simply want to open that file (whether it is an .xls file,
.txt file, etc.). I am attempting to open the file using the
"System.IO.File.Open" command, but nothing seems to happen. Any ideas?

Thanks in advance!

************************************************** ******

private void btnOpenFile_Click(object sender, System.EventArgs e)
{
string strExt = this.txtFileExtension.Text;
strExt = strExt + " Files|*." + strExt;
this.openFileDialog1.InitialDirectory = @"C:\";
this.openFileDialog1.Filter = strExt;
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
txtSource.Text = openFileDialog1.FileName;
else
txtSource.Text = "";

System.IO.File.Open(txtSource.Text, FileMode.Open,
FileAccess.Read, FileShare.None);
}


*** Sent via Developersdex http://www.developersdex.com ***

Apr 25 '06 #3

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

Similar topics

7
by: Scott Chapman | last post by:
Hi! I'd like a "file" on the Linux box to actually be the input and output of a Python script. Anything written to the file would be sent to a database and anything read from the file would...
9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
10
by: aagee | last post by:
Hi all Can someone clue me in on this? I just noticed that following doesn't work as it used to: window.open(... "fullscreen=yes" ...) Used to get a window that occupied the whole screen;...
5
by: Mr Gordonz | last post by:
Hi all, I want to put a button on a page, and when the user clicks it, the standard Windows "Open File" dialogue box opens, and the user can browse/select a file on their PC. Having selected a...
7
by: Drew Berkemeyer | last post by:
I've encounted a pretty strange problem and I'm not quite sure what to make of it. I have a web service that consumes an XML file as well as a few other parameters. This web service works fine...
3
by: Terry Olsen | last post by:
I followed the information in a previous thread, but i'm having problems. I have a couple dropdownlists and a submit button on my form. The user selects choices from the lists and clicks the...
2
by: jerry.ranch | last post by:
I'm starting to learn about the tab control. How would I write an on open event procedure in VBA, that upon opening of the form, a specific tab opens (say tab 1)? Thanks Jerry
4
by: arajunk | last post by:
In Firefox this opens a full size window (maximized) . In IE it opens the partial window requiring user to click restore (upper right) to maximize. What am I missing ? var...
2
by: jjlagtap | last post by:
Hey everyone When I try to open a file i get the Exception listed below. The weird thing is it works when I run the web app locally and when i use a remote server and open a file on my computer....
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.