Click here to Skip to main content
12,765,284 members (37,252 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#

I am tring to write code to open a user file using openfiledialog:
private FileStream OpenFile()
            // Displays an OpenFileDialog and shows the read/only files.

            OpenFileDialog dlgOpenFile = new OpenFileDialog();
            dlgOpenFile.ShowReadOnly = true;

            if (dlgOpenFile.ShowDialog() == DialogResult.OK)

                // If ReadOnlyChecked is true, uses the OpenFile method to
                // open the file with read/only access.

                if (dlgOpenFile.ReadOnlyChecked == true)
                    return (FileStream)dlgOpenFile.OpenFile();


                // Otherwise, opens the file with read/write access.

                    string path = dlgOpenFile.FileName;
                    return new FileStream(path, System.IO.FileMode.Open,
            return null;

The file does not open and I do not know what I am doing wrong. I get the filedialog but that is it.

I need to be able to open files with any extention e.g. Word, excel, images,text etc.

Can anybody help me please?
Posted 22-Apr-12 13:17pm
Nelek 22-Apr-12 18:31pm
I am not sure to really understand you, you want to open any file, but... what is it going to bring you if you don't know in which format are saving the data? Opening a file is not a big issue, but understanding what it is inside... this is another thing.
SAKryukov 22-Apr-12 18:52pm
What makes you think that the file is not open? In what case (read-only or not)?
SAKryukov 22-Apr-12 19:17pm
Well, I provided some missing detail on opening file in my advice, but... you are right. OP does not really understand what to read, it looks like.
Anyway, please see my answer.
milenalukic 22-Apr-12 19:34pm
My clients would receive files from colleagues that will be saved in a specific folder. All I want to do is to allow them to open this folder from within the windows application and to open any file they have received. The received files could be anything.

So I want the facility like in windows explorer to select a file and open it in read write mode.
milenalukic 22-Apr-12 19:46pm
The reason I think the file is not open is because it does not show on screen. I think that is quite obvious and what any user would expect when they click on open.
SAKryukov 23-Apr-12 21:57pm
This is not a serious reason to make any statement. You need to take correct observation. Basically, you need to use the debugger to say for sure. Your second statement shows deeply wrong approach to programming in general...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The code you show has nothing to do with your goal "to open files any extension", but no code has anything to do with that. More exactly, extension do not matter (and, in modern file systems, there is no such thing as "extension", there is only some naming conventions to suggest (only suggest) a "file type"). What does matter, is the file format; and you cannot say "any file format". Of course, you can "open" (after all, what is "open", in this respect) and read anything at all, for example, as an array of bytes, but why?

So, the goal formulated as such, not just realistic or unrealistic, it simply makes no sense. It looks you are lost pretty well.

Now, the signature of your file is not practical, if breaks the symmetry open-close. It will make difficult applying safe practice working with files, which would guarantee closing of file handles:
try {
   // open file
   // read file
} finally {
   // close file

You also the homogenous principle in opening the file. Why read-only file is opened with different APIs? It makes the code less supportable. So, to improve this, you should open the file in the construct shown schematically above, via new FileStream in both cases. Even better, for read-only case use System.IO.StreamReader or System.IO.BinaryReader. These classes implement System.IDisposable, so you can apply using statement for automatic disposable. Please see:[^],[^],[^].

So, your dialog method should only return the file name and readonly flag, not the file.

However, all this would make no sense if you don't do read operation and know exactly what do you want to read. I don't think you know that at this moment. Maybe I just wasted my time for explaining the detail of opening file above?

Wes Aday 22-Apr-12 19:29pm
milenalukic 22-Apr-12 19:55pm
Yes the dialoge does return the filename. All I want to know is how to open the file from there. And no I am not lost or confused in what I need to do but I need some help in trying to do it as explained above. I have no way of knowing what the file type could be as it could be anything a user has received.
SAKryukov 22-Apr-12 20:20pm
No, the dialog does not return the file name. Only a function return value, and dialog class is a class, it returns what you call. Again, you are lost. Name it confused, I see no difference. Can you be just logical and finally admit that "any file type" (or "extension") makes no sense in principle. Imagine tomorrow yet another stupid company creates yes another idiotic file format, and what, you want to read it, too? At least this is how your question sounds. If you need something you can formulate without those "etc." and "e.g.", ask about it. Here is the case where "etc." and "e.g." are poorly incorrect.
SAKryukov 22-Apr-12 20:21pm
Thank you, Wes, but I'm afraid OP cannot recognize that... I'm afraid it will be difficult to explain things... :-)
milenalukic 22-Apr-12 20:26pm
It will only open files where the user has the relevant program to open it with just as windows explorer does.

If I try to open an xlsx file and do not have excel 2007 it will obviously not open, but if it is a txt file it should open with notepad or whatever you have set your pc to have a default for that type.

That is all I want and would appreciate some sample code rather than sarcastic comments please.
Wes Aday 23-Apr-12 9:37am
"it should open with notepad " if you would have said that to begin with you would have been simply pointed to the System.Diagnostics.Process class without all the grief.
milenalukic 23-Apr-12 9:54am
Thanks for that Wes, but would be an easy solution which I could have easily found sample code.

What is seems I am not getting through to you both is that I have no way of knowing what file types my users would have received from colleagues. It may be an Excel file, a word document, a presentation an image or whatever else may be required.

What I was hoping to achieve is to have an option in my application to open received files folder, from where the user can open any received file, provided he has the software to do so. I would expect an error if the required program was not installed, similar to what you get from windows explorer, but otherwise if I have a word document and word is installed I would want this to open. Same goes for any file type. If this is not possible just let me know.

thanks again
Wes Aday 23-Apr-12 10:40am
Funny thing is, the Process class does exactly that. I am not seeing what it is that you are having such a hard time with this. The code that you have posted only creates a FileStream which does not have anything to do with opening a file using its file association.
milenalukic 23-Apr-12 10:44am
So how would I need to do this. Can you send me a sample code please?

I assumed this would be eaasy enough but found nothing on the net.
Wes Aday 23-Apr-12 10:46am
System.Diagnostics.Process pr = new System.Diagnostics.Process();
pr.StartInfo.UseShellExecute = true;
pr.StartInfo.FileName = yourfilename;
catch (Exception ex)
if there is no file association, an exception will be thrown.
milenalukic 23-Apr-12 10:53am
Perfect. Just what I needed. thanks
SAKryukov 23-Apr-12 21:59pm
Great. You are very welcome.
In this case, please accept the answer formally (green button) -- thanks.
milenalukic 24-Apr-12 2:42am
Yes I will as soon as Wes adds his solution as he was the one who gave me what I needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 22 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100