询问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