没有创建文件时出现异常System.IO.IO.Exception

时间:2016-08-30 10:04:05

标签: visual-studio-2012

运行以下代码时:

Public Class Form4
'Public LAST_USER As String = "last user: uidxxxxx"
Friend WithEvents LastUser As System.Windows.Forms.TextBox
Private Sub checkFile()
    If File.Exists("D:\Tebenator\userid.txt") = False Then
        Try
            File.Create("D:\Tebenator\userid.txt")
            ' Info()
        Catch ex As Exception
            MsgBox("Exception:" + ex.ToString)
        End Try
    Else
        Info()
    End If

End Sub
Private Sub Info()

    Dim line As String
    Dim objReader As New System.IO.StreamReader("D:\Tebenator\userid.txt")
    Do While objReader.Peek() <> -1
        line = objReader.ReadLine
        If (String.IsNullOrEmpty(line.ToString)) Then
            TextBox1.Text = " Last session info:" & Environment.NewLine +
                    "-Last user : uidxxxxx" & Environment.NewLine +
                                "-Session closed: (manual or automatically at xx:xx (local)"
            Exit Do
        Else
            If (line.StartsWith("last_user")) Then
                Dim a() As String
                a = Split(line, "uid")
                If (String.IsNullOrWhiteSpace(a.ToString)) Then
                    TextBox1.Text = " Last session info:" & Environment.NewLine +
                     "-Last user : uidxxxxx" & Environment.NewLine +
                                 "-Session closed: (manual or automatically at xx:xx (local)"
                Else

                    For i As Integer = 0 To a.Length - 1
                        Dim info As System.IO.FileInfo
                        info = My.Computer.FileSystem.GetFileInfo("D:\Tebenator\userid.txt")
                        TextBox1.Text = " Last session info:" & Environment.NewLine +
                        "-Last user :uid" + a(i) & Environment.NewLine +
                                    "-Session closed: (manual or automatically on " + info.LastWriteTime + ")"

                    Next

                End If
            End If
        End If
    Loop
    objReader.Close()

End Sub
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'checkFile()
    Projects.Items.Add("A")
    Projects.Items.Add("B")
    Projects.Items.Add("C")
End Sub
Private Sub PictureBoxLogo_Click(sender As Object, e As EventArgs) Handles PictureBoxLogo.Click
    Dim path As String
    path = System.IO.Path.GetDirectoryName( _
       System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

    'System.Diagnostics.Process.Start(path & "\kill tebenator.exe")
End Sub

Private Sub LinkLabelMapDrives_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabelMapDrives.LinkClicked
    Form2.Show()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Button1.FlatAppearance.BorderColor = Color.Orange
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Button2.FlatAppearance.BorderColor = Color.Orange
    If MessageBox.Show("Confirm SHUT DOWN action?", "Tebenator Demon: Shutting down ..", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
        Me.Close()
    End If
End Sub


Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Button3.FlatAppearance.BorderColor = Color.Orange
    Form3.Show()
End Sub


Private Sub ButtonStartSession_Click(sender As Object, e As EventArgs) Handles ButtonStartSession.Click

    Dim Path1 As String
    Path1 = "D:\users\Popa Andrei\project\Prj_A"
    Dim Path2 As String
    Path2 = "D:\users\Popa Andrei\project\Prj_B"
    Dim Path3 As String
    Path3 = "D:\users\Popa Andrei\project\Prj_C"
    Dim id As Integer
    id = Convert.ToInt32(TextBoxUid.Text)
    If (id > 9999) Then
        MessageBox.Show("Uid too long,please modify.", "Error!", MessageBoxButtons.OK)
    ElseIf (id < 1000) Then
        MessageBox.Show("Uid too short,pleaste modify.", "Error!", MessageBoxButtons.OK)
    Else
        Dim SelectedItem As Object

        SelectedItem = Projects.SelectedItem

        If (SelectedItem.ToString() Is "A") Then
            Dim Folder As String = My.Computer.FileSystem.SpecialDirectories.Desktop + " \Dir_Project_" + SelectedItem.ToString

            If Directory.Exists(Folder) = False Then
                Try
                    Directory.CreateDirectory(Folder)
                Catch ex As Exception

                End Try
            End If
            For Each _File As String In Directory.GetFiles(Folder)
                File.Delete(_File)
            Next
            My.Computer.FileSystem.CopyDirectory(Path1, Folder.ToString, True)
            Process.Start(Folder)
        End If
        If (SelectedItem.ToString() Is "B") Then
            Dim Folder As String = My.Computer.FileSystem.SpecialDirectories.Desktop + " \Dir_Project_" + SelectedItem.ToString

            If Directory.Exists(Folder) = False Then
                Try
                    Directory.CreateDirectory(Folder)
                Catch ex As Exception

                End Try
            End If
            For Each _File As String In Directory.GetFiles(Folder)
                File.Delete(_File)
            Next
            My.Computer.FileSystem.CopyDirectory(Path2, Folder.ToString, True)
            Process.Start(Folder)
        End If
        If (SelectedItem.ToString() Is "C") Then
            Dim Folder As String = My.Computer.FileSystem.SpecialDirectories.Desktop + " \Dir_Project_" + SelectedItem.ToString

            If Directory.Exists(Folder) = False Then
                Try
                    Directory.CreateDirectory(Folder)
                Catch ex As Exception

                End Try
            End If
            For Each _File As String In Directory.GetFiles(Folder)
                File.Delete(_File)
            Next
            My.Computer.FileSystem.CopyDirectory(Path3, Folder.ToString, True)
            Process.Start(Folder)
        End If

        Dim File_Name As String = "D:\Tebenator\userid.txt"
        Try
            If File.Exists(File_Name) Then
                Dim objWriter As New System.IO.StreamWriter(File_Name, True)
                objWriter.Write("last_user=uid" + TextBoxUid.Text)
                objWriter.Close()
            End If
        Catch ex As Exception
            MsgBox("Exception:" + ex.ToString)

        End Try

        End If
End Sub

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    checkFile()
End Sub
End Class

表单正确加载,我键入uid值然后选择项目,但是当我单击Start Session时,我在第168行得到以下错误(Dim objWriter As New System.IO.StreamWriter(File_Name,True)):< / p>

类型&#39; System.IO.IOException&#39;的第一次机会异常发生在mscorlib.dll

1 个答案:

答案 0 :(得分:0)

正确的方法是修改checkFile方法,如下所示:

Private Sub checkFile()
    If File.Exists("D:\Tebenator\userid.txt") = False Then
        Try
            Dim f As FileStream = File.Create("D:\Tebenator\userid.txt")
            f.Close()
        Catch ex As Exception
            MsgBox("Exception:" + ex.ToString)
        End Try
    Else

    End If

End Sub