473,703 Members | 2,766 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 4417
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
3799
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 would probably have to be run as a daemon. I doubt there's enough magic in the Linux world to make...
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
11179
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
3608
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 server. BUT: I don't want to use an <input type="file"> control.
7
2672
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. However, I get an exception if I use an <input type="file"> control on the ASP page that allows...
3
1331
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 prompt him to save it as a file. Here's the code.
2
1946
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
3259
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
7949
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# - filepath is a valid file name which is passed via the form. System.IO.BinaryReader br = new...
0
8659
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
9239
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...
0
9102
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8995
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8951
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6585
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
5920
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();...
1
3113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.