Must click twice to get correct output from GetLastWriteTime

Posted on 2005-04-28
Last Modified: 2006-11-18
I have a button click that fires a function that runs a .bat file. The bat file updates a zip. The web page has a label that displays the timestamp of that zip file. The problem is the button has to be clicked twice to get the correct time displayed. The first click _will run everything...if i look at the file on the server, it has the correct time...but the label doesn't show that until i click the button again.
<asp:button id=cra onclick=cra_Click runat="server" Text="Repackage"></asp:button>

    Sub cra_Click(ByVal sender As Object, ByVal e As EventArgs)
    End Sub

Function DoPackage(ByVal pack)
      ' Get the full file path
      Dim strFilePath As String = Server.MapPath("\MyPath\") & pack & ".bat"
      ' Create the ProcessInfo object
      Dim psi As New System.Diagnostics.ProcessStartInfo("cmd.exe")
      psi.UseShellExecute = False
      psi.RedirectStandardOutput = False
      psi.RedirectStandardInput = True
      psi.RedirectStandardError = True
      psi.WorkingDirectory = Server.MapPath("\MyPath\")
      ' Start the process
      Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.Start(psi)
      ' Open the batch file for reading
      Dim strm As System.IO.StreamReader = System.IO.File.OpenText(strFilePath)
      ' Attach the output for reading
      'Dim sOut As System.IO.StreamReader = proc.StandardOutput
      ' Attach the in for writing
      Dim sIn As System.IO.StreamWriter = proc.StandardInput
      ' Write each line of the batch file to standard input
      While strm.Peek() <> -1
      End While
      ' Exit CMD.EXE
      Dim stEchoFmt As String = "# {0} run successfully. Exiting"
      sIn.WriteLine([String].Format(stEchoFmt, strFilePath))
      ' Close the process
      ' Close the io Streams;
End Function

Function WritePackLabels()
      If (System.IO.File.Exists(Server.MapPath("\MyPath\"))) Then
      lbmyFile.Text = System.IO.File.GetLastWriteTime(Server.MapPath("\MyPath\")).ToString()
      End If
End Function
Question by:juststeve
    LVL 13

    Expert Comment

    Try setting your autopostback = true for your button

    Author Comment

    where would I set that?
    LVL 33

    Expert Comment

    ?? buttons are always "autopostback", I don't think that is his problem.  It almost seems like the file doesn't exist when the time comes to update the label.
    LVL 33

    Accepted Solution

    I think you need to "WaitForExit" on your Process, it's demonstrated here.

       Private Sub btnWaitForExit_Click( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) _
          Handles btnWaitForExit.Click
          ' create a new process
          Dim myProcess As Process = _
          ' wait until it exits
          ' display results
          MessageBox.Show("Notepad was closed at: " & _
             myProcess.ExitTime & "." & _
             System.Environment.NewLine & "Exit Code: " & _
       End Sub

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now