[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Must click twice to get correct output from GetLastWriteTime

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)
        DoPackage("cra")
    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
      sIn.WriteLine(strm.ReadLine())
      End While
      strm.Close()
      ' Exit CMD.EXE
      Dim stEchoFmt As String = "# {0} run successfully. Exiting"
      sIn.WriteLine([String].Format(stEchoFmt, strFilePath))
      sIn.WriteLine("EXIT")
      ' Close the process
      proc.Close()
      ' Close the io Streams;
      sIn.Close()
      WritePackLabels()
End Function

Function WritePackLabels()
      If (System.IO.File.Exists(Server.MapPath("\MyPath\myFile.zip"))) Then
      lbmyFile.Text = System.IO.File.GetLastWriteTime(Server.MapPath("\MyPath\myFile.zip")).ToString()
      End If
      .
      .
      .
End Function
0
juststeve
Asked:
juststeve
  • 2
1 Solution
 
TorrwinCommented:
Try setting your autopostback = true for your button
0
 
juststeveAuthor Commented:
where would I set that?
0
 
raterusCommented:
?? 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.
0
 
raterusCommented:
I think you need to "WaitForExit" on your Process, it's demonstrated here.
http://www.devx.com/dotnet/Article/7914/1954?pf=true

   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 = _
         System.Diagnostics.Process.Start("sample.txt")
   
      ' wait until it exits
      myProcess.WaitForExit()
   
      ' display results
      MessageBox.Show("Notepad was closed at: " & _
         myProcess.ExitTime & "." & _
         System.Environment.NewLine & "Exit Code: " & _
         myProcess.ExitCode)
      myProcess.Close()
   End Sub
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now