首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SaveFileDialog.OpenFile() IndexOutofRangeException

SaveFileDialog.OpenFile() IndexOutofRangeException
EN

Stack Overflow用户
提问于 2013-10-31 21:31:52
回答 2查看 897关注 0票数 0

我最近写了一些与菜单栏相关的代码,有一个选项包含OpenFile、SaveFile和Exit等。下面是代码。

代码语言:javascript
复制
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "ebd files (*.txt)|*.txt"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True
    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        Try
            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
                Dim sw As New StreamWriter(myStream)

                sw.Flush()
                sw.Close()

                myStream.Close()
             End If
         Catch Ex As Exception
             MessageBox.Show("Can't save file on disk: " & Ex.Message)
         End Try
    End If

另一段代码与of If语句完全相同,如下所示,

代码语言:javascript
复制
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "ebd files (*.txt)|*.txt"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    Try
        myStream = saveFileDialog1.OpenFile()
        If (myStream IsNot Nothing) Then
            ' Code to write the stream goes here.
            Dim sw As New StreamWriter(myStream)
            'some sw.WriteLine code here...            
            sw.Flush()
            sw.Close()

            myStream.Close()
         End If
     Catch Ex As Exception
         MessageBox.Show("Can't save file on disk: " & Ex.Message)
     End Try

我遇到的问题是第二段代码,当它运行时,系统会将索引抛出范围之外的异常,如何不使用If语句来修复它?是否有任何方法显示对话框窗口?有人能给我关于这个索引错误信息的线索吗?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-31 22:05:58

查看OpenFile的源代码可以发现可能的错误点。

代码语言:javascript
复制
public Stream OpenFile()
{
    IntSecurity.FileDialogSaveFile.Demand();
    string str = base.FileNamesInternal[0];
    if (string.IsNullOrEmpty(str))
    {
        throw new ArgumentNullException("FileName");
    }
    Stream stream = null;
    new FileIOPermission(FileIOPermissionAccess.AllAccess, IntSecurity.UnsafeGetFullPath(str)).Assert();
    try
    {
        stream = new FileStream(str, FileMode.Create, FileAccess.ReadWrite);
    }
    finally
    {
        CodeAccessPermission.RevertAssert();
    }
    return stream;
}

代码似乎试图获取base.FileNamesInternal[0];,但是如果您不显示对话框或选择一个文件名来保存这个内部数组,则可能是空的。

我尝试读取属性FileNames中索引为零的项,并得到相同的错误。

代码语言:javascript
复制
string file = saveFileDialog1.FileNames(0)  ' Index out of range....

如果真的是这样的话,我想听听我们的WinForms更有经验的海报。

票数 0
EN

Stack Overflow用户

发布于 2013-10-31 22:30:42

我想我有点搞不懂你想要做什么。

在第二个示例中,您似乎从未“显示”对话框--即saveFileDialog1.ShowDialog()

因此,用户无法选择文件/路径。

然后尝试使用尚未设置的文件/路径。

http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.openfile.aspx

如果您只需要打开一个用于写的任意文件,而不向用户显示对话框,那么为什么不直接使用File.Open()之类的呢?

http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19716962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档