在c#win窗体中使用openfiledialog打开文件 [英] open a file using openfiledialog in c# win forms

查看:85
本文介绍了在c#win窗体中使用openfiledialog打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写代码以使用openfiledialog打开用户文件:

 私有 FileStream OpenFile()
        {
            // 显示OpenFileDialog并显示只读文件.

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


            如果(dlgOpenFile.ShowDialog()== DialogResult.OK)
            {

                // 如果ReadOnlyChecked为true,则使用OpenFile方法
                // 以只读/只读访问权限打开文件.

                如果(dlgOpenFile.ReadOnlyChecked ==  true )
                {
                    返回(FileStream)dlgOpenFile.OpenFile();

                }

                // 否则,打开具有读/写访问权限的文件.

                其他
                {
                    字符串 path = dlgOpenFile.FileName;
                    返回  FileStream(路径,System.IO.FileMode.Open,
                            System.IO.FileAccess.ReadWrite);
                }
            }
            返回 ;
        } 



该文件无法打开,我不知道自己在做什么错.我得到了文件对话框,但仅此而已.

我需要能够以任何扩展名打开文件,例如Word,Excel,图像,文本等.

有人可以帮我吗?那.更确切地说,扩展名无关紧要(并且,在现代文件系统中,没有扩展名"之类的东西,只有一些命名约定可以建议(仅建议)文件类型").重要的是文件格式;并且您不能说任何文件格式".当然,您可以打开"(在这方面,毕竟是打开")并完全读取任何内容,例如,作为字节数组,但是为什么?

因此,这样制定的目标,不仅是现实的或不现实的,根本没有任何意义.看来您迷路了.

现在,如果打破对称性,文件的签名将变得不切实际.应用安全实践处理文件将很困难,这将确保关闭文件句柄:

 尝试 {
   // 打开文件
   // 读取文件
} 最终 {
   // 关闭文件
} 


您在打开文件时也遵循同质原则.为什么用不同的API打开只读文件?它使代码的支持性降低.因此,为了改善这一点,在两种情况下都应通过new FileStream在上面示意性显示的结构中打开文件.更好的是,对于只读情况,请使用System.IO.StreamReaderSystem.IO.BinaryReader.这些类实现了System.IDisposable,因此您可以将using语句应用于自动一次性处理.请参阅:
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 [ ^ ].

因此,您的dialog方法应该只返回文件名和只读标志,而不返回文件.

但是,如果您不执行读取操作并且确切知道您想读取什么内容,那么所有这些都将毫无意义.我认为您目前不知道这一点.也许我只是在浪费时间解释上面打开文件的细节?

—SA


Hi,

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.

                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?

解决方案

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:
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


这篇关于在c#win窗体中使用openfiledialog打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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