vb.net Windows 7 中的 mscorlib.dll 中发生了“System.IO.IOException”类型的第一次机会异常,但 xp 上没有

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23840861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:35:55  来源:igfitidea点击:

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll in windows 7 but not on xp

.netvb.net

提问by croquis24

I am having a bit of a problem with this code that works fine in Visual Basic 2010 Professional under Windows XP using .NET 4.0. However, when this code runs on a Windows 7 machine, I get the following error:

我在使用 .NET 4.0 的 Windows XP 下的 Visual Basic 2010 Professional 中使用此代码时遇到了一些问题。但是,当此代码在 Windows 7 机器上运行时,我收到以下错误:

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll

"An Unhandled Exception Of Type 'System.IO.IOException' Occurred In Mscorlib.dll"

mscorlib.dll 中发生了“System.IO.IOException”类型的第一次机会异常

“Mscorlib.dll 中发生类型为‘System.IO.IOException’的未处理异常”

The code I have written is used to write the content of a ListBox to a text file.

我编写的代码用于将 ListBox 的内容写入文本文件。

The problem is that the file is not created or when it is created manually, it is not read from and this only happens in Windows 7.

问题是文件没有被创建,或者当它被手动创建时,它没有被读取,这只会发生在 Windows 7 中。

This is the code I have written:

这是我写的代码:

   'at the very top
   Imports System.Net
   Imports System
   Imports System.Windows.Forms
   Imports System.Security.Permissions

   'in my form load
   If My.Computer.FileSystem.FileExists("C:\hitory.txt") Then
        Dim h As New IO.StreamReader("C:\hitory.txt")
        While (h.Peek() > -1)
            history.ListBox1.Items.Add(h.ReadLine)
        End While
        h.Close()
    Else
        'charge l'historique en mémtheitroade
        Dim w As New IO.StreamWriter("C:\hitory.txt")
        Dim i As Integer
        For i = 0 To history.ListBox1.Items.Count - 1
            w.WriteLine(history.ListBox1.Items.Item(i))
        Next
        w.Close()
    End If

    If My.Computer.FileSystem.FileExists("C:\fav.txt") Then
        Dim f As New IO.StreamReader("C:\fav.txt")
        While (f.Peek() > -1)
            fav.ListBox1.Items.Add(f.ReadLine)
        End While
        f.Close()
    Else
        'charge les favorit en mémtheitroade
        Dim z As New IO.StreamWriter("C:\fav.txt")
        Dim j As Integer
        For j = 0 To fav.ListBox1.Items.Count - 1
            z.WriteLine(fav.ListBox1.Items.Item(j))
        Next
        z.Close()
    End If
    textBox1.Items.AddRange(IO.File.ReadAllLines("C:\hitory.txt"))

回答by Cody Gray

On the Windows 7 machine, UAC is kicking in. Your application does not have the appropriate privileges to write to files in the root level of the disk.

在 Windows 7 机器上,UAC 正在发挥作用。您的应用程序没有适当的权限来写入磁盘根级别中的文件。

The bug was still there in Windows XP, it just wasn't being caught with your (default) security settings. Any competent system administrator would have configured the system to restrict access to the root level of the disk, and you'd be right back where you are under Windows 7.

该错误仍然存​​在于 Windows XP 中,只是您的(默认)安全设置没有发现它。任何称职的系统管理员都会将系统配置为限制对磁盘根级别的访问,并且您将立即回到 Windows 7 下的位置。

There are only two possible fixes:

只有两种可能的修复方法:

  1. Add a manifest to your application that indicates it must be run with administrative privileges. This will cause UAC to prompt for elevation when the application is launched. If your user account has administrative privileges, you'll be able to elevate the process and then it will have the privileges to write to the root level of the disk. Otherwise, you're screwed—you can't even launch the app.

    This is the wrongchoice. Your application should notbe writing to the root level of the disk, so papering over the central bug by introducing a bunch of additional complexity is, frankly, stupid.

  2. Fix the code so that it saves files some place else. The Application Data folder is a good place, unless the end user needs to be able to see/modify the files, then you want the My Documents folder.

    You get the path to these folders using the System.Environment.GetFolderPathmethod, which takes an eumeration value of type Environment.SpecialFolderthat specifies the folder you want to retrieve the path to.

    Good choices are:

    • SpecialFolder.ApplicationDatafor application-specific data that should roam with the user account.

    • SpecialFolder.LocalApplicationDatafor application-specific data that should notroam with the user account.

    • SpecialFolder.MyDocumentsfor documents created by the application that the user should be able to see/modify.

    Once you get the path, use the System.IO.Path.Combinemethodto append your file name. Use thatpath instead to load/save the file.

  1. 向您的应用程序添加一个清单,表明它必须以管理权限运行。这将导致 UAC 在应用程序启动时提示提升。如果您的用户帐户具有管理权限,您将能够提升该进程,然后它将拥有写入磁盘根级别的权限。否则,你就完蛋了——你甚至不能启动应用程序。

    这是错误的选择。您的应用程序应该写入磁盘的根级别,因此坦率地说,通过引入一堆额外的复杂性来掩盖中心错误是愚蠢的。

  2. 修复代码,使其将文件保存在其他地方。Application Data 文件夹是个好地方,除非最终用户需要能够查看/修改文件,否则您需要 My Documents 文件夹。

    您可以使用System.Environment.GetFolderPath方法获取这些文件夹的路径,该方法采用Environment.SpecialFolder指定要检索路径的文件夹的类型的枚举值。

    好的选择是:

    • SpecialFolder.ApplicationData对于应该随用户帐户漫游的特定于应用程序的数据。

    • SpecialFolder.LocalApplicationData对应的应用程序特定的数据与用户账号漫游。

    • SpecialFolder.MyDocuments对于由应用程序创建的用户应该能够查看/修改的文档。

    获得路径后,使用该System.IO.Path.Combine方法附加文件名。使用路径来加载/保存文件。

Oh yeah, and spell the file name correctly: history.txt. :-)

哦,是的,正确拼写文件名:history.txt. :-)

回答by Matthew Mitchell

Dim w As New IO.StreamWriter("C:\hitory.txt")*

This code says that you are opening a StreamWriteryou need to close that before you can continue. Add at the end of your coding w.close()before you do anything else with the fill. It is kind of like opening a movie and watching it. Then moving it somewhere else while it is still open. It will not work.

此代码表示您正在打开一个StreamWriter您需要关闭它才能继续。在w.close()对填充执行任何其他操作之前,在编码的末尾添加。这有点像打开一部电影并观看它。然后在它仍然打开时将它移到其他地方。不起作用。

回答by porkchop

"C:\hitory.txt"

"C:\history.txt"

typo is probably your issue. put the whole thing in a try, catch:

错字可能是你的问题。把整个事情放在尝试中,抓住:

Try
    ' code
Catch ax As UnauthorizedAccessException
    MsgBox("ax: " & ax.ToString)
Catch ix As IO.IOException
    MsgBox("ix: " & ix.ToString)
Catch ex As Exception
    MsgBox("ex: " & ex.ToString)
End Try

and to append, specify the 'append' parameter. ie:

要追加,请指定“追加”参数。IE:

Dim w As New IO.StreamWriter("C:\history.txt", True)

also, why use My.Computer.FileSystem, and not System.IO? it's faster, less abstraction.

另外,为什么使用 My.Computer.FileSystem 而不是 System.IO?它更快,抽象更少。

also, i don't like this peek stuff. i don't see a need for it in your case, see my other post here for StreamReader/Writer usage: https://stackoverflow.com/questions/23792819/vb-code-to-parse-one-file-into-two-files/23816245#23816245

另外,我不喜欢这种偷看的东西。我认为在您的情况下不需要它,请参阅我的其他帖子以了解 StreamReader/Writer 的用法:https: //stackoverflow.com/questions/23792819/vb-code-to-parse-one-file-into-两个文件/23816245#23816245

or there's this other approach, which will return a String() array, which you can easily convert to a dataset or list for your listbox: http://msdn.microsoft.com/en-us/library/s2tte0y1%28v=vs.110%29.aspx

或者还有其他方法,它将返回一个 String() 数组,您可以轻松地将其转换为列表框的数据集或列表:http: //msdn.microsoft.com/en-us/library/s2tte0y1%28v=vs .110%29.aspx

File.ReadAllLines("C:\history.txt")

edit: as codebased mentioned in comments (i can't write comments yet), permissions are a possible issue as well.

编辑:正如评论中提到的基于代码的(我还不能写评论),权限也是一个可能的问题。