HOME > .NET Framework > Forum > ไม่สามารถ delete file pdf ได้ ใช้ asp.net ( the process cannot access the file because it is being used by another process)
ไม่สามารถ delete file pdf ได้ ใช้ asp.net ( the process cannot access the file because it is being used by another process)
ไม่สามารถลบ file pdf ได้ โดยมี error ขึ้นมา " the process cannot access the file because it is being used by another process"
ช่วยหน่อยนะคะ
Code (VB.NET)
If txtSubject.Text = String.Empty Then
ScriptManager.RegisterStartupScript(Page, Page.GetType, "AlertMessage", "alert('Please enter Subject.');", True)
txtSubject.Focus()
ElseIf txtTo.Text = String.Empty Then
ScriptManager.RegisterStartupScript(Page, Page.GetType, "AlertMessage", "alert('Please enter To.');", True)
txtTo.Focus()
ElseIf txtDetail.Text = String.Empty Then
ScriptManager.RegisterStartupScript(Page, Page.GetType, "AlertMessage", "alert('Please enter Detail.');", True)
txtDetail.Focus()
ElseIf txtCC.Text = String.Empty Then
ScriptManager.RegisterStartupScript(Page, Page.GetType, "AlertMessage", "alert('Please enter CC.');", True)
txtCC.Focus()
Else
Try
Dim rpts As CrystalDecisions.CrystalReports.Engine.ReportDocument = CType(Session("ReportDocument"), CrystalDecisions.CrystalReports.Engine.ReportDocument)
Dim pathpdf As String = Server.MapPath(".") & "\PDFDownload\MogasAndKerosene_" & Session(USERNAME) & ".pdf"
' Dim pathpdf As String = Server.MapPath(".") & "\PDFDownload\MogasAndKerosene.pdf"
Dim crExportOptions As ExportOptions
Dim crDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim crFormatTypeOptions As New PdfRtfWordFormatOptions()
crDiskFileDestinationOptions.DiskFileName = pathpdf
crFormatTypeOptions.FirstPageNumber = 1
crFormatTypeOptions.LastPageNumber = 3
crFormatTypeOptions.UsePageRange = True
crExportOptions = rpts.ExportOptions
With crExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.ExportDestinationOptions = crDiskFileDestinationOptions
.ExportFormatOptions = crFormatTypeOptions
End With
Try
rpts.Export()
rpts.Close()
rpts.Dispose()
rpts = Nothing
Catch ex As Exception
End Try
'Step SendMail
Dim mail As New clsMail
' Dim mailSubject As String = "Mogas and Kerosene Update -" & txtDateNow.Text
Dim mailForm As String = "<[email protected]>"
' Dim mailTo As String = "[email protected]"
' Dim mailCC As String = "[email protected]"
Dim mailAtt As String = pathpdf
Dim localPath As String = Server.MapPath("")
Dim IsResult As Boolean = False
' IsResult = mail.SendMailFinish(localPath, mailForm, mailTo, mailCC, mailAtt, mailSubject)
IsResult = mail.SendMailFinish(localPath, mailForm, mailAtt, txtSubject.Text, txtDetail.Text, txtTo.Text, txtCC.Text)
If IsResult = True Then
ScriptManager.RegisterStartupScript(Page, Page.GetType, "AlertMessage", "alert('Send Email Successfull');", True)
Else
ScriptManager.RegisterStartupScript(Page, Page.GetType, "AlertMessage", "alert('Send Email Unsuccess');", True)
End If
Catch ex As Exception
End Try
tdEdit.Visible = False
txtSubject.Text = String.Empty
txtDetail.Text = String.Empty
txtCC.Text = String.Empty
txtTo.Text = String.Empty
'Dim FileEmail As New FileInfo(Server.MapPath(".") & "\PDFDownload\MogasAndKerosene_" & Session(USERNAME) & ".pdf")
'If FileEmail.Exists Then
' FileEmail.Delete()
'Else
'End If
Dim filePaths As String() = Directory.GetFiles(Server.MapPath(".") & "\PDFDownload\", "*.pdf")
For Each filePath As String In filePaths
File.Delete(filePath)
Next
End If