【发布时间】:2014-05-24 02:53:24
【问题描述】:
我在使用 .NET 4.0 的 Windows XP 下的 Visual Basic 2010 Professional 中运行良好的这段代码有点问题。但是,当此代码在 Windows 7 机器上运行时,我收到以下错误:
在 mscorlib.dll 中发生了“System.IO.IOException”类型的第一次机会异常
“在 Mscorlib.dll 中发生类型为 'System.IO.IOException' 的未处理异常”
我写的代码是用来将ListBox的内容写入文本文件的。
问题是文件没有被创建,或者当它被手动创建时,它没有被读取,这只发生在 Windows 7 中。
这是我写的代码:
'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émoire
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émoire
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"))
【问题讨论】:
-
查看文件夹和运行程序的权限;否则还要检查病毒扫描程序不是这里的瓶颈。