詢問VB2010 啟動外部程式有何限制呢?

Home Home
引用 | 編輯 sob790717
2014-06-13 21:51
樓主
推文 x0
請問 VB2010 要呼叫外部程式
他可能會有那些安全性的因素而封鎖
導致不讓我執行呢?

同樣都是執行 WinRAR 文字版的 UnRar.exe

使用 Process.Start 屢 ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 ebolaman
2014-06-14 23:54
1樓
  


1. Process.Start 不可混入參數,否則系統會當成是路徑的一部分
要加參數要用 ProcessStartInfo 設置,參考範例

2. 編程可以用很多方法來壓縮/解壓
a. SDK: ex. 7zip 提供的 LZMA
b. 函式庫 (library): ex. 範例中使用 DotNetZip
c. 外部呼叫: 直接呼叫執行檔幫忙解壓

操縱性: a > b > c
效率: a > b > c
穩定性: a > b > c
方便性: a < b < c

範例中實現了 b 和 c, 如果是快速開發,建議選 c (也就是你原本用的方式)

3. 不建議用 WinRar, 因為是商業軟體,建議用 7zip


範例程式碼:
複製程式
Imports System.IO
Imports Ionic.Zip


Module Module1
    ' from QuickTime official website
    Private Const SAMPLE_FILE As String = "sample_mpeg4.mp4.zip"


    Public Sub Main()
        ' delete extracted file
        File.Delete("sample_mpeg4.mp4")


        ' use DotNetZip
        'ExtractByDotNetZip()


        ' use 7zip command line version
        'ExtractByCall()


        ' use Process.Start the wrong way
        ExtractWrong()


        ' pause
        Console.WriteLine("Finished.")
        Console.ReadKey()
    End Sub


    Private Sub ExtractByDotNetZip()
        Using zip As ZipFile = ZipFile.Read(SAMPLE_FILE)
            For Each entry As ZipEntry In zip
                Try
                    entry.Extract(ExtractExistingFileAction.OverwriteSilently)
                Catch ex As Exception
                    Console.WriteLine("Error {0}", ex.Message)
                End Try
            Next
        End Using
    End Sub


    Private Sub ExtractByCall()
        Dim _
            psi As _
                New ProcessStartInfo _
                With {.FileName = "7za.exe",
                .Arguments = String.Format("x {0} -aoa", SAMPLE_FILE)
                }
        Dim ret As Integer


        Using proc As Process = Process.Start(psi)
            proc.WaitForExit()
            ret = proc.ExitCode
        End Using


        If 0 = ret Then
            Console.WriteLine("OK!")
        Else
            Console.WriteLine("Error = {0}", ret)
        End If
    End Sub


    Private Sub ExtractWrong()
        Using _
            proc As Process =
                Process.Start(String.Format("7za.exe x {0} -aoa",
                                            SAMPLE_FILE))
            proc.WaitForExit()
        End Using
    End Sub
End Module

1. 測試檔案可從 http://support.apple.com/kb/ht1425 取得

2. DotNetZip: http://dotnetzip.codeplex.com/

3. 7Zip 命令列 (7-Zip Command Line Version): http://www.7-zip.org/download.html

4. 外部呼叫方式:

複製程式
Dim psi As New ProcessStartInfo With {.FileName = "檔案名", .Arguments = "參數"}
Process.Start(psi)


4. 使用 proc.WaitForExit() 等待程序結束,為同步的方式,接下來才能用 proc.ExitCode 取得 ExitCode,可得知程式執行結果,ex. 7za 回傳 0 代表沒錯

本帖包含附件
檔名: zip ConsoleApplication1.part1.rar   (2022-06-09 14:21 / 782 KB)  
範例專案 (同範例程式碼)
下載次數:1

本帖包含附件
檔名: zip ConsoleApplication1.part2.rar   (2022-06-09 14:21 / 782 KB)   下載次數:3

本帖包含附件
檔名: zip ConsoleApplication1.part3.rar   (2022-06-09 14:21 / 353 KB)   下載次數:2


獻花 x1
引用 | 編輯 ebolaman
2014-06-22 17:12
2樓
  
術語像是 wait, pause, suspend, sleep 都是指暫停一段時間,一般上 UI 和程式都會暫止
.NET 在這方面比較屬於 thread 的操控

不管用什麼方法,
目標是消耗低 CPU,盡量不要讓 UI 當掉,還有保持正確性

獻花 x0