是的,这是一个经常出现的主题。 但我已经尝试了帖子给出的大部分建议。 例如使用'using'语句,构建循环结构,错误处理,用于处理删除的单独函数,使用路径检查等。 但我一直在收到错误。代码如下。 我试过了:
Protected Sub ImageButton3_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton3.Click
If 1 = 1 Then
'impersonateValidUser(System.Configuration.ConfigurationManager.AppSettings("UserID").ToString(), "DOMAIN.COMPANY.COM", System.Configuration.ConfigurationManager.AppSettings("FilePW").ToString()) Then
Dim fileLoc As String = System.Configuration.ConfigurationManager.AppSettings("FileLocation").ToString()
Dim fileLoc2 As String = System.Configuration.ConfigurationManager.AppSettings("FileLocationtemp").ToString()
Dim fileLoc3 As String = System.Configuration.ConfigurationManager.AppSettings("FileArchiveLocation").ToString()
'Dim outputfle As StreamWriter
Dim strline As String = ""
Dim carrier As String
Dim order_no As String
Dim transId As String = ""
Dim FileToTest As String
Dim FileToDelete As String
Dim cn As New OleDbConnection("Provider=SQLOLEDB;" & SqlDataSource3.ConnectionString)
Dim sqlstr As String = "SELECT top 10 * FROM [ export view]"
Dim cmd As New OleDbCommand(sqlstr, cn)
cmd.CommandTimeout = 999999
cmd.CommandType = Data.CommandType.Text
Try
cn.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader()
Dim sb As StringBuilder = New StringBuilder()
carrier = ""
order_no = ""
If dr.HasRows Then
transId = GridView1.Rows(0).Cells(4).Text
End If
If transId <> "" Then
Using outputfle = New StreamWriter(fileLoc2 & transId & ".COMPANYm")
While (dr.Read())
sb = New StringBuilder()
For col = 0 To dr.FieldCount - 1
If Not dr.IsDBNull(col) Then
If Not dr.GetName(col) = "order_no" Then
Dim removestr As String = dr.GetValue(col).ToString().Replace("""", " ").Trim
sb.Append(removestr.Replace(",", " ").Trim)
sb.Append(vbTab)
End If
Else
sb.Append(vbTab)
End If
If dr.GetName(col) = "order_no" Then
order_no = dr.GetValue(col).ToString
ElseIf dr.GetName(col) = "carrier" Then
carrier = dr.GetValue(col).ToString
End If
'col = col + 1
Next
outputfle.WriteLine(sb.ToString)
Dim updatesql As String = "UPDATE company_shipping_loads SET upload= '1' WHERE (order_no = '" & order_no & "' )"
Dim updatecm As OleDbCommand = New OleDbCommand(updatesql, cn)
updatecm.ExecuteNonQuery()
End While
dr.Dispose()
outputfle.Close()
'it allowed end using here!
End Using
End If
Dim NumberOfRetries As Integer
Dim DelayOnRetry As Integer
Dim i As Integer
Dim index As Integer
NumberOfRetries = 3
DelayOnRetry = 1000
'NumberOfRetries()
Try
For index = 1 To NumberOfRetries
index = index + 1
'for (index As Integer =1; i <= NumberOfRetries; ++i)
FileToTest = fileLoc2 & transId & ".COMPANYm"
FileToDelete = fileLoc2 & transId & ".COMPANY"
If funcFileExists(FileToTest) = True Then
Console.WriteLine(FileToTest & " existed")
' If File.Exists(fileLoc2 & transId & ".COMPANYm") Then
'File.Move(fileLoc2 & transId & ".TXT", fileLoc & transId & ".TXT")
'File.Move(fileLoc2 & transId & ".COMPANYm", fileLoc & transId & ".txt")
'File.Copy(fileLoc2 & transId & ".COMPANYm", "\\firstbitofpath\secondbitofpath\3rdbitofpath\4thbitofpath\" & transId & ".txt")
File.Copy(fileLoc2 & transId & ".COMPANYm", fileLoc3 & transId & ".txt")
File.Move(fileLoc2 & transId & ".COMPANYm", fileLoc & transId & ".txt")
Console.WriteLine("Ready for delete")
funcDeleteFile(FileToDelete)
Console.WriteLine("File " & FileToDelete & " should now be deleted")
End If
Next
Catch ex As Exception
' You may check error code to filter some exceptions, not every error
' can be recovered.
If (index = NumberOfRetries) Then
'// Last one, (re)throw exception and exit
Throw
Dim strFile1 As String = System.Configuration.ConfigurationManager.AppSettings("LogFile").ToString() '"C:\Websites\yourfile.txt"
Dim fileExists1 As Boolean = File.Exists(strFile1)
'strFile.
Using sw1 As New StreamWriter(File.Open(strFile1, FileMode.Append))
' Using sw As New StreamWriter(File.AppendText(strFile))
sw1.WriteLine( _
IIf(fileExists1, _
"We tried " & NumberOfRetries.ToString() & "times, but we could not conduct the file operations. Error msg may follow:" & ex.ToString(), _
"Start Error Log for today " & DateTime.Now & vbNewLine & ex.ToString()))
End Using
Threading.Thread.Sleep(DelayOnRetry) 'Sleep(DelayOnRetry)
End If
Finally
'Do Nothing
End Try
'end of new bit
cmd.Connection.Close()
cn.Close()
GridView1.DataBind()
Dim intzero As Integer
Dim intnumber As Integer
Dim result As Integer
intzero = 0
intnumber = 1
result = intnumber / intzero
Catch ex As Exception
Response.Write(ex.Message)
Dim strFile As String = System.Configuration.ConfigurationManager.AppSettings("LogFile").ToString() '"C:\Websites\yourfile.txt"
Dim fileExists As Boolean = File.Exists(strFile)
Dim st As New StackTrace(True)
st = New StackTrace(ex, True)
'strFile.
Using sw As New StreamWriter(File.Open(strFile, FileMode.Append))
' Using sw As New StreamWriter(File.AppendText(strFile))
sw.WriteLine( _
IIf(fileExists, _
"Error Message in Occured at-- " & DateTime.Now & ex.ToString() & "Line that errored: " & st.GetFrame(0).GetFileLineNumber().ToString(), _
"Start Error Log for today " & DateTime.Now & vbNewLine & ex.ToString()))
End Using
Try
'outputfle.Close()
Catch es As Exception
End Try
End Try
GridView1.DataBind()
Else
Dim message As String = "You are not able to create the file. please contact system admin"
ClientScript.RegisterStartupScript(Me.GetType(), "file creation", "<script " & _
"language=JavaScript>alert('" & message & "');</script>")
End If
End Sub
功能:
Function funcFileExists(ByVal FileToTest As String) As Boolean
funcFileExists = (Dir(FileToTest) <> "")
End Function
Sub funcDeleteFile(ByVal FileToDelete As String)
If funcFileExists(FileToDelete) Then 'See above
' First remove readonly attribute, if set
SetAttr(FileToDelete, vbNormal)
' Then delete the file
Kill(FileToDelete)
End If
End Sub