Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am tring to write code to open a user file using openfiledialog:
C#
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.

                else
                {
                    string path = dlgOpenFile.FileName;
                    return new FileStream(path, System.IO.FileMode.Open,
                            System.IO.FileAccess.ReadWrite);
                }
            }
            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
Comments
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.
Sergey Alexandrovich Kryukov 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.
--SA
Sergey Alexandrovich Kryukov 22-Apr-12 18:52pm    
What makes you think that the file is not open? In what case (read-only or not)?
--SA
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.

1 solution

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:
C#
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:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

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?

—SA
 
Share this answer
 
Comments
[no name] 22-Apr-12 19:29pm    
Nice
Sergey Alexandrovich Kryukov 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... :-)
--SA
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.
Sergey Alexandrovich Kryukov 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.
--SA
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900