开发者

ASP.NET VBscript add a cmd line?

Have this upload script, and it works. But I would like to add a cmd command after the upload has complete. Is this possible? Thank you in advance.

<%@ Page Language=VBScript %>

    <script runat="server">
        Protected Sub Button1_Click(ByVal sender As Object, _
          ByVal e As System.EventArgs)
            If FileUpload1.HasFile Then
                Try
                    FileUpload1.SaveAs("C:\Inetpub\wwwroot\upload\" & _
                       FileUpload1.FileName)
                    Label1.Text = "File name: " & _
                       FileUpload1.PostedFile.FileName & "<br>" & _
                       "File Size: " & _
                       FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                       "Content type: " & _
                       FileUpload1.PostedFile.ContentType
                Catch ex As Exception
                    Label1.Text = "ERROR: " & ex.Message.ToString()
                End Try
            Else
  开发者_如何学JAVA              Label1.Text = "You have not specified a file."
            End If
        End Sub
    </script>


Use ProcessStartInfo:

C#

public static int ExecuteCommand(string Command, int Timeout)
{
   int ExitCode;
   ProcessStartInfo ProcessInfo;
   Process Process;

   ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
   ProcessInfo.CreateNoWindow = true; 
   ProcessInfo.UseShellExecute = false;
   Process = Process.Start(ProcessInfo);
   Process.WaitForExit(Timeout);
   ExitCode = Process.ExitCode;
   Process.Close();

   return ExitCode;
}

VB:

Public Shared Function ExecuteCommand(Command As String, Timeout As Integer) As Integer
Dim ExitCode As Integer
Dim ProcessInfo As ProcessStartInfo
Dim Process As Process

ProcessInfo = New ProcessStartInfo("cmd.exe", "/C " + Command)
ProcessInfo.CreateNoWindow = True
ProcessInfo.UseShellExecute = False
Process = Process.Start(ProcessInfo)
Process.WaitForExit(Timeout)
ExitCode = Process.ExitCode
Process.Close()

Return ExitCode
End Function

To use it in your example:

Put the function outside of your sub (like right above it) and put the following line where you'd like the code to execute.

ExecuteCommand("REN C:\Document.rtf YES.rtf",100)

You can check the return value (0 for success) to see if it was successful.

To do without using the command line:

Change your line that saves to file to the following:

FileUpload1.SaveAs("C:\Inetpub\wwwroot\upload\" & _
                   "myFile.txt")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜