如何在vb.net中压缩错误'它已被另一个进程使用' [英] how to compress the error 'it is already used by another process' in vb.net
问题描述
我使用函数将水晶报告输出到pdf文件。
首先它运行正常。但下次我收到此错误:
System.IO.Exception,它说该文件正在被另一个进程使用
这是我的编码:
Iam using a function to export a crystal report to pdf file.
First it is running fine. but next time iam getting this error:
System.IO.Exception and it is saying the file is being used by another process
HERE IS MY CODING:
Private Sub GetReport()
Dim strSQL As String
Dim objReport As New frmReport
Try
strSQL = "SP_rpt_SalesRptByMainCategory '" & Now.ToLongDateString & "','" & Now.ToLongDateString & "'," & gBranchCode
DIM p AS STRING
p = AppDomain.CurrentDomain.BaseDirectory
p = p.Replace("\bin\", "\Reports\")
cryRpt.Load(p & "rptMainCat.rpt")
Dim objds As DataSet = ExexcuteSQLQuery(strSQL)
cryRpt.SetDataSource(objds.Tables(0))
objReport.CrystalReportViewer2.ReportSource = cryRpt
Dim CrExportOptions As ExportOptions
Dim CrDiskFileDestinationOptions As New _
DiskFileDestinationOptions()
Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
'Dim FileToDelete As String
'FileToDelete = Application.StartupPath & "\MainCategoryReport.pdf"
'If System.IO.File.Exists(FileToDelete) = True Then
' System.IO.File.Delete(FileToDelete)
'End If
StrFilePath = "\MainCategoryReport" & "_" & Now.ToLongDateString & ".pdf"
CrDiskFileDestinationOptions.DiskFileName = Application.StartupPath & StrFilePath '' "c:\Report1.pdf"
CrExportOptions = cryRpt.ExportOptions
With CrExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.DestinationOptions = CrDiskFileDestinationOptions
.FormatOptions = CrFormatTypeOptions
End With
cryRpt.Export()
Catch ex As Exception
MsgBox(ex.ToString)
Exit Sub
End Try
End Sub
Private Shared Function ExexcuteSQLQuery(ByVal StrSql As String) As DataSet
Dim objDA As SqlDataAdapter = New SqlDataAdapter(StrSql, objForEMailConnection)
Dim objDS As DataSet = New DataSet
objDA.Fill(objDS, "myDataset")
Return objDS
End Function
提前致谢
Nirmala Saravanan
Thanks in advance
Nirmala Saravanan
推荐答案
这很容易调查。
首先,类似的问题在这里被多次提出,根据我的经验,我知道:在大多数情况下,阻止过程是你自己的过程。您可能忘记在同一个应用程序中处理/关闭某些内容。所以,首先,检查一下。要探索这种可能性,请参阅我过去的答案:
清除C#中的句柄 [ ^ ]。
在这个答案中,注意使用/ code>语句可以帮助您保证在使用后正确处理相应的文件系统对象,而不是保持文件锁定。
在某些情况下,你真的需要调查哪个进程拥有哪个文件。为此,我建议使用 Sysinternals Suite 中的一个实用程序。这组实用程序(以前来自 Winternals 公司,目前在Microsoft)是必须的对于任何开发人员,请参阅:
http://technet.microsoft.com/en -us / sysinternals / bb842062 [ ^ ],
http://technet.microsoft.com/en-us/sysinternals / bb545027 [ ^ ]。
您需要的实用程序是 handle.exe ,请参阅:
http://technet.microsoft.com/en-us/sysinternals/bb896655 [ ^ ]。
在您的情况下,您使用文件名参数:
This is easy to investigate.
First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].
In this answer, pay attention for the using of theusing
statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.
In some cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].
The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].
In your case, you use it with file name parameter:
handle.exe <file_name>
此实用程序将扫描所有类型的句柄,而不仅仅是文件句柄。对于文件,它将扫描与文件名匹配的所有文件句柄(因此它不必是完整路径名)并返回足以识别每个进程的信息,包括其 pid 。因此,如果您需要有关相关流程的更多信息,您还可以使用其他Sysinternals实用程序,特别是其 Process Explorer :
http://technet.microsoft.com/en-us/sysinternals/bb896653 [ ^ ]。
祝你好运,
This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].
Good luck,
这篇关于如何在vb.net中压缩错误'它已被另一个进程使用'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!