Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using OpenFileDialog,how to identify that wrong format of input file is chosen?My input file is only for text file to undergo process data,whenever a wrong format of file is chosen for open, a messagebox will pop up "Invalid Format!".Is it possible to make it?Thanks.
Posted
Comments
[no name] 31-Jul-12 22:01pm    
It's not the job of the OpenFileDialog to determine the format of the file. That is your job as the programmer. Is it possible to do what you think you want to do? Yes, it's possible. The only real way to know that the file is the right format is to open it and try and read it. You cannot be sure that a file is a text file just from the file extension. What are you going to do if your user renames a .jpg to .txt?
Esmond90 31-Jul-12 22:09pm    
Thanks.Sorry i am just a beginner and I learn myself.The problem I face now is whenever a text file is opened,the program running is fine.But if the user choose a wrong format such as .jpg,I just wish a messagebox is pop up to indicate wrong format or file.=)

This is trivial. You have the file name. Check the extension to see if it matches. If the extension matches but the file is not valid, you can't tell that until you try to open the file.
 
Share this answer
 
Comments
Esmond90 31-Jul-12 22:28pm    
Oh I see,is it possible for me to filter format input for drag and drop class?when wrong format is dragged in, messagebox pop up?
Esmond90 31-Jul-12 22:30pm    
If the extension matches but file is not valid to be read, can i add a messagebox to indicate that?
Christian Graus 31-Jul-12 23:20pm    
Yes, you can do anything you like. However, if you do what someone suggested above ( sorry, I thought this was obvious ), they can still type *.* and load any file they want. You need to write your code that reads the file so that if the file is invalid, it shows a message.
Esmond90 31-Jul-12 23:38pm    
Oh, I just don't know how to write code to determine an invalid file or whenever a data cannot be read,for both for openfiledialog and drag-n-drop.
Christian Graus 31-Jul-12 23:43pm    
You're essentially going to put your file read code in a try/catch, as well as testing assumptions about your format.
You can set the Filter property of the dialog object. This will insure that the exact file type will be selected from the dialog. E.g
C#
openFileDialog1.Filter = "txt files (*.txt)|*.txt;"// Will filter only text file

For more see MSDN. FileDialog.Filter Property[^]
 
Share this answer
 
Comments
Esmond90 31-Jul-12 22:24pm    
Oh.Thanks!I can do it this way too.=) But can I filter for the drag and drop class too?
Wonde Tadesse 2-Aug-12 19:55pm    
Sure you can. See this youtube tutor.http://www.youtube.com/watch?v=UmAL5JYNCVg

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