Asked by:
Check whether file is Open

Question
-
User-511102329 posted
Hi all i am using VS 2005 for web application development.
How to check whether file is aleardy opened in vb.net code and if it is open,how to close that file.
Pls help me.
Wednesday, July 2, 2008 1:24 AM
All replies
-
User-368260749 posted
Hi,
Try the following;
try
{
using(FileStream fs = File.OpenWrite(strFileName)
{
if(fs == null)
return;
}
}
catch(UnauthorizedAccessException e)
{
//Here you know that the file is open by another app
}I don't think it is possible to close the file on behalf of another program... that would be kind of illegal :) Hope that this helps...
Wednesday, July 2, 2008 2:56 AM -
User-511102329 posted
Hi
Thanks for reply.
Actually i am using the following code to convert .rdlc file into Pdf file and i am opening that pdf file.
But when user clicks on submit button first time it will create pdf file and opens that file.
Second time when the user clicks on submit button without closing already opened Pdf file,it is giving error.
But my requirement is I need to check whether that pdf is already opened,if it is opened,I want to display one message.Like pdf is already opened,pls close it first.
My code is:
FolderLocation = System.AppDomain.CurrentDomain.BaseDirectory
Dim filepath As String = FolderLocation & "OrderRequestCnt.pdf" ' If File.Exists(filepath) ThenFile.Delete(filepath)
'End If Dim sql As StringReportViewer1.ProcessingMode = ProcessingMode.Local
sql =
"SELECT COUNT(O.ORDERNO) AS COUNT1,D.DEPARTMENTNAME FROM TFMS_ORDER_REQUESTS O LEFT JOIN TFMS_DEPARTMENT_MASTER D ON D.DEPARTMENTID=O.DEPARTMENTID" If ddlDeptName.SelectedValue <> "" Thensql = sql &
" WHERE D.DEPARTMENTID='" + ddlDeptName.SelectedValue + "'"Flag =
"S" End If If txtFromdate.Value <> "" Then If Not Flag = "" Thensql = sql &
" AND TO_CHAR(O.ORDERDATE,'DD-MON-YY')>=TO_DATE('" + txtFromdate.Value + "','DD-MON-YY')" Elsesql = sql &
" WHERE TO_CHAR(O.ORDERDATE,'DD-MON-YY')>=TO_DATE('" + txtFromdate.Value + "','DD-MON-YY')"Flag =
"S" End If End If If txtTodate.Value <> "" Then If Not Flag = "" Thensql = sql &
" AND TO_CHAR(O.ORDERDATE,'DD-MON-YY')<=TO_DATE('" + txtTodate.Value + "','DD-MON-YY')" Elsesql = sql &
" WHERE TO_CHAR(O.ORDERDATE,'DD-MON-YY')<=TO_DATE('" + txtTodate.Value + "','DD-MON-YY')"Flag =
"S" End If End Ifsql = sql &
" GROUP BY D.DEPARTMENTNAME"sql = sql &
" order by D.DEPARTMENTNAME"myDataSet = GetRequestordercnt(sql)
Dim count As Integer count = myDataSet.Tables("TFMS_DEPARTMENT_MASTER").Rows.Count If count > 0 ThenlblNoRecords.Visible =
FalseReportViewer1.Visible =
False Dim rep As LocalReport = ReportViewer1.LocalReportrep.ReportPath = System.AppDomain.CurrentDomain.BaseDirectory &
"Reports\OrderrequestCount.rdlc" Dim ds As New ReportDataSource()ds.Name =
"OrderRequestCnt_TFMS_DEPARTMENT_MASTER" ds.Value = myDataSet.Tables("TFMS_DEPARTMENT_MASTER")rep.DataSources.Add(ds)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
Response.ContentType =
"Application/PDF"ReportViewer1.LocalReport.Refresh()
System.Diagnostics.Process.Start(filepath)
Response.WriteFile(filepath)
Response.Redirect("OrderRequestCountDsg.aspx") ElseReportViewer1.Visible =
FalselblNoRecords.Visible =
TruelblNoRecords.Text =
"No Records Found" End If End SubPls help me.
Wednesday, July 2, 2008 4:52 AM -
User-368260749 posted
Ok, so when you try to open the file use the following code to catch the exception and use a MessageBox.Show() to display your message!
catch(UnauthorizedAccessException e)
{
//Enter error message
}If you have any problems let me know
Wednesday, July 2, 2008 6:03 AM