locked
Check whether file is Open RRS feed

  • 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:

     

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    Dim warnings As Warning() = Nothing

    Dim streamids As String() = Nothing

    Dim mimeType As String = Nothing

    Dim encoding As String = Nothing

    Dim extension As String = Nothing

    Dim bytes As Byte()

    Dim FolderLocation As String

    Dim Flag As String

    FolderLocation = System.AppDomain.CurrentDomain.BaseDirectory

    Dim filepath As String = FolderLocation & "OrderRequestCnt.pdf"

    ' If File.Exists(filepath) Then

    File.Delete(filepath)

    'End If

    Dim sql As String

    ReportViewer1.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 <> "" Then

    sql = sql & " WHERE D.DEPARTMENTID='" + ddlDeptName.SelectedValue + "'"

    Flag = "S"

    End If

    If txtFromdate.Value <> "" Then

    If Not Flag = "" Then

    sql = sql & " AND TO_CHAR(O.ORDERDATE,'DD-MON-YY')>=TO_DATE('" + txtFromdate.Value + "','DD-MON-YY')"

    Else

    sql = 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 = "" Then

    sql = sql & " AND TO_CHAR(O.ORDERDATE,'DD-MON-YY')<=TO_DATE('" + txtTodate.Value + "','DD-MON-YY')"

    Else

    sql = sql & " WHERE TO_CHAR(O.ORDERDATE,'DD-MON-YY')<=TO_DATE('" + txtTodate.Value + "','DD-MON-YY')"

    Flag = "S"

    End If

    End If

    sql = 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 Then

    lblNoRecords.Visible = False

    ReportViewer1.Visible = False

    Dim rep As LocalReport = ReportViewer1.LocalReport

    rep.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)

     

    '' To View in PDF Format

    bytes = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)Dim fs As New FileStream(FolderLocation & "OrderRequestCnt.PDF", FileMode.Create)

    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")

    Else

    ReportViewer1.Visible = False

    lblNoRecords.Visible = True

    lblNoRecords.Text = "No Records Found"

    End If

    End Sub

     

    Pls 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