如何在文件对话框中设置大小限制 [英] How to set the Size limit in a filedialog

查看:122
本文介绍了如何在文件对话框中设置大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我已经开发了一个寡妇应用程序,它可以连接到基于Internet的数据库.这是一个"OpenFileDialog" ...我可以在其属性中设置文件大小的限制吗?

谢谢

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

推荐答案

OpenFileDialog没有文件大小限制.但是,当用户选择文件时,您可以检查文件大小.

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

if (DialogResult.OK == openFileDialog1.ShowDialog())
{
   FileInfo fi = new FileInfo(openFileDialog1.FileNamefile);
   long fileSize = fi.Length; //The size of the current file in bytes.file 
}


修改对话框行为超出其属性的唯一官方方法是System.Windows.Forms.FileDialog.HookProc.这是非常棘手的,并且不能在平台之间移植,因为涉及特定于平台的消息.

我知道在CodeProject上有一些有趣的作品,例如:在.NET中扩展保存文件对话框类 [ ^ ](SaveFileDialogOpenFileDialog —并不重要),自定义Windows通用文件打开对话框 [
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


使用FileOK事件并添加此代码
Use the FileOK event and add this code
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    FileInfo info = new FileInfo(openFileDialog1.FileName);
    if (info.Length > "yourFilesize")
        e.Cancel=true;
}


这篇关于如何在文件对话框中设置大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆