无法从磁盘读取文件

当我从vb应用上传一个excel文件的时候,第一次没事。 如果我再试一次,我有这个错误: 进程无法访问文件(path),因为它被另一个进程使用 。 以下是我的代码:

Dim fd As OpenFileDialog = New OpenFileDialog() fd.Title = "Open File Dialog" fd.InitialDirectory = "C:\" fd.Filter = "All files (*.xlsx)|*.xlsx|All files (*.xlsx)|*.xlsx" fd.FilterIndex = 2 fd.RestoreDirectory = True Dim myStream As Stream = Nothing If fd.ShowDialog() = DialogResult.OK Then Try myStream = fd.OpenFile() If (myStream IsNot Nothing) Then workbook = APP.Workbooks.Open(fd.FileName) worksheet = workbook.Worksheets("sheet1") TextBox1.Text = worksheet.Cells(1, 7).Value TextBox2.Text = worksheet.Cells(2, 7).Value TextBox3.Text = worksheet.Cells(3, 7).Value l1.Text = worksheet.Cells(2, 1).Value * 10 w1.Text = worksheet.Cells(2, 2).Value * 10 q1.Text = worksheet.Cells(2, 3).Value p1.Text = worksheet.Cells(2, 4).Value.......... Dim Values(119, 3) As String Values(0, 0) = l1.Text Values(0, 1) = w1.Text Values(0, 2) = q1.Text Values(0, 3) = p1.Text......... Dim add As Integer = 0 Dim pressing As Integer = 0 If adding.Text = "50" Or adding.Text = "" Then add = 50 Else add = Convert.ToInt16(adding.Text) End If If press.Text = "20" Or press.Text = "" Then pressing = 20 Else pressing = Convert.ToInt16(press.Text) End If Dim l As Integer Dim w As Integer Dim machinearea As Integer Dim connetionString As String Dim cnn As SqlConnection connetionString = "Data Source=.;Initial Catalog=lumber;User ID=sa;Password=sasql" cnn = New SqlConnection(connetionString) Dim cmd As SqlCommand Dim myreader As SqlDataReader Dim query As String query = "SELECT length,width from marea" cmd = New SqlCommand(query, cnn) cnn.Open() myreader = cmd.ExecuteReader() If myreader.Read() Then l = myreader.Item("length") w = myreader.Item("width") End If cnn.Close() machinearea = l * w Dim allTextBoxes2 = From txt In Me.Panel1.Controls.OfType(Of TextBox)() Order By txt.TabIndex Dim txtList2 = allTextBoxes2.ToList() For i As Int32 = 0 To txtList2.Count - 1 Dim thisTxt = txtList2(i) Dim nextIndex = If(i + 1 >= txtList2.Count, 0, i + 1) Dim prevIndex = If(i - 1 < 0, txtList2.Count - 1, i - 1) Dim nextTxt = txtList2(nextIndex) Dim prevTxt = txtList2(prevIndex) Dim testInt As Integer = 0 If thisTxt.Text = "0" Then thisTxt.Clear() End If Next Dim allTextBoxes = From txt In Me.Panel1.Controls.OfType(Of TextBox)() Order By txt.TabIndex Dim txtList = allTextBoxes.ToList() For i As Int32 = 0 To txtList.Count - 1 Dim thisTxt = txtList(i) For j = 2 To -1 For k = 1 To 4 thisTxt.Text = worksheet.Cells(i + j, k).Value Next Next Next End If Catch Ex As Exception MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message) Finally ' Check this again, since we need to make sure we didn't throw an exception on open. If (myStream IsNot Nothing) Then myStream.Close() End If End Try End If 

  Try myStream = fd.OpenFile() If (myStream IsNot Nothing) Then workbook = APP.Workbooks.Open(fd.FileName, ReadOnly:=True, Notify:=False) worksheet = workbook.Worksheets("sheet1") 

  Finally ' Check this again, since we need to make sure we didn't throw an exception on open. If (myStream IsNot Nothing) Then myStream.Close() End If If (workbook IsNot Nothing) Then workbook.Close() End If End Try 

确保应用程序已closures,没有进程正在运行,否则会出现此错误。