473,573 Members | 3,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Cli ck(object sender, System.EventArg s e)
{
string strExt = this.txtFileExt ension.Text;
strExt = strExt + " Files|*." + strExt;
this.openFileDi alog1.InitialDi rectory = @"C:\";
this.openFileDi alog1.Filter = strExt;
if (openFileDialog 1.ShowDialog() != DialogResult.Ca ncel)
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 4392
OutdoorGuy <Ou********@fis hing.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.Diagnost ics.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.co m>
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.Diagnost icsProcess.Star t(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_Cli ck(object sender, System.EventArg s e)
{
string strExt = this.txtFileExt ension.Text;
strExt = strExt + " Files|*." + strExt;
this.openFileDi alog1.InitialDi rectory = @"C:\";
this.openFileDi alog1.Filter = strExt;
if (openFileDialog 1.ShowDialog() != DialogResult.Ca ncel)
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
3790
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 come from the database. I know how to do the database end of this but I'm not sure if a script can be hooked to a "file" transparently. The script...
9
715
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 anyway).... Private Sub LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) If e.ChangeType = WatcherChangeTypes.Created Then
10
11146
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; now I get the title bar and status bar too. Any ideas?
5
3590
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 file, the user clicks Open, and I then want to capture the file path and name in a string variable. Finally, I want to upload the file to the...
7
2646
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 if I use the web test interface. It also works fine if I call it from an ASP.NET page that has a text box where the XML is pasted and then passed on....
3
1326
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 submit button. The app then places the data requested into a string variable. I want to send this data out to the client and have the save dialog...
2
1941
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
3226
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 infoWindow=window.open('usgsMain.html','USGSwindow','status=no,resizable=yes,scrollbars=yes');
2
7935
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. When someone else uses the web app and tries to open a file using the remote server the error below happens. any help? **Code** C# -...
0
7641
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...
0
8155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7705
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6338
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5527
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...
0
3673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
976
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...

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.