Opening a File using "File.Open"

  • Thread starter Thread starter OutdoorGuy
  • Start date Start date
O

OutdoorGuy

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);
}
 
OutdoorGuy said:
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.)
 
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
 

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