2011-07-25 67 views
1

有這個上傳腳本,它的工作原理。但是我想在上傳完成後添加一個cmd命令。這可能嗎?先謝謝你。ASP.NET VBscript添加一個cmd行?

<%@ 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 
       Label1.Text = "You have not specified a file." 
      End If 
     End Sub 
    </script> 
+0

你是什麼意思「添加CMD按鈕」究竟?請更清楚。另外,'ContentLength'屬性返回的長度是*字節*而不是千字節,你必須在1024中分割得到KB。 –

+0

請解釋「添加cmd命令」的含義,很難說明你指的是什麼。你是否試圖從shell運行一個外部命令? –

+0

我的意思是想插入一個命令,例如「REN C:\ Document.rtf YES.rtf」。 – user791276

回答

1

使用的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 

要在你的榜樣使用它:

把函數的子之外(就像在它上面)和pu t您希望代碼執行的以下行。

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

您可以檢查返回值(0成功),看它是否是成功的。

要做到不使用命令行:

更改您的生產線,節省了到文件到以下幾點:

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

謝謝傑夫。我應該在哪裏插入這個? – user791276

+0

查看編輯答案。 –

+0

我會避免調用一個命令行選項,並在代碼中重命名該文件...我將此添加到我的答案 –