Click here to Skip to main content
15,850,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've developed a widows application which connects to internet based database. Here is one " OpenFileDialog"...Can i set the limit of file size in its properties?

Thank You
Posted

OpenFileDialog does not have file size limit. But when a user selects a file, you can check the file size.

C#
if (DialogResult.OK == openFileDialog1.ShowDialog())
{
   FileInfo fi = new FileInfo(openFileDialog1.FileNamefile);
   long fileSize = fi.Length; //The size of the current file in bytes.file 
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Mar-11 14:59pm    
That is correct, but does not solve the problem as OP wants.
My 5, because the problem should not be solved, in my opinion.
--SA
Sergey Alexandrovich Kryukov 10-Mar-11 15:07pm    
Just for giggling, see my answer.
--SA
Yusuf 10-Mar-11 15:34pm    
Thanks for accepting the answer. Good Luck with the rest!
Ian Good 10-Mar-11 17:48pm    
Voted down because, while it works, using the FileOk event handler (see apaka's solution) is the preferred method.
Yusuf 10-Mar-11 22:22pm    
Go head and down vote as much as you like. I am one of those who give sh*t to the rep points. Won't loose sleep because of univotes.

Since when is the "preferred" method the ONLY way. How the hell will you handle it in .net 3.5 or earlier where such events don't exist? Didn't we see all the time the mantra of MS changing all the time. It is pathetic to justify your vote. I don't care about any vote anyway. Just state this way or that way is the preferred way.
The only official way to modify dialog's behavior beyond its property is System.Windows.Forms.FileDialog.HookProc. This is quite tricky and not portable between platforms, because platform-specific messages are involved.

I know some interesting works on CodeProject, like these ones: Extending the save file dialog class in .NET[^] (SaveFileDialog or OpenFileDialog — does not really matter), Customizing the Windows Common File Open Dialog[^].

—SA
 
Share this answer
 
v5
Comments
Dalek Dave 10-Mar-11 18:35pm    
Good Answer
Sergey Alexandrovich Kryukov 10-Mar-11 21:08pm    
Thank you.
--SA
Use the FileOK event and add this code
C#
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    FileInfo info = new FileInfo(openFileDialog1.FileName);
    if (info.Length > "yourFilesize")
        e.Cancel=true;
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Mar-11 15:05pm    
There is not such event to use this handler. You did not show the event, where is that?
--SA
DEB4u 10-Mar-11 15:09pm    
this size is in bytes?
apaka 10-Mar-11 15:16pm    
OpenFileDialog control has two events FileOK and HelpRequest. In FileOK event you can check your file size(it's in butes) and if it doesnd match your requements just set e.Cancel to true and the file wouldn't be selected. Works for me. I'm using Net4 maybe thats the diference.
Sergey Alexandrovich Kryukov 10-Mar-11 15:39pm    
Gosh, I missed it! My apologies! Good answer, my 5.
--SA
Dalek Dave 10-Mar-11 18:35pm    
Good Call

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