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

Opening a File using "File.Open"

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.