簡體   English   中英

使用 Process.Start C# 通過快捷方式運行應用程序

[英]Run application via shortcut using Process.Start C#

有沒有辦法通過 C# 應用程序的快捷方式運行應用程序?

我正在嘗試從我的 C# 應用程序運行 a.lnk。 該快捷方式包含大量 arguments,我希望應用程序不必記住它們。

嘗試通過 Process.Start 運行快捷方式會導致異常。

謝謝

編輯:

異常是“Win32Exception”:“指定的可執行文件不是有效的 Win32 應用程序。”

這是(縮寫)代碼:

ProcessStartInfo info = new ProcessStartInfo ( "example.lnk" );
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
Process whatever = Process.Start( info );

你能發布一些代碼。 像這樣的東西應該工作:

Process proc = new Process();
proc.StartInfo.FileName = @"c:\myShortcut.lnk";
proc.Start();

設置UseShellExecute = false是問題所在。 一旦我刪除它,它就停止崩潰了。

如果您使用 UseShellExecute = false 並嘗試啟動批處理文件,請確保將 add.bat 添加到文件名的末尾。 如果 UseShellExecute = true 你不需要.bat。 這讓我浪費了一個小時的工作……希望能救別人。

如果您的文件是 EXE 或其他文件類型,如“.exe”或“.mkv”或“.pdf”,並且您想使用快捷鏈接運行它,您的代碼必須像這樣。

我想運行“Translator.exe”程序。

Process.Start(@"C:\Users\alireza\Desktop\Translator.exe.lnk");

我今天花了幾個小時試圖解決這個問題,幸運的是在另一個網站上找到了答案,所以如果這對你有幫助,請務必投票給我的答案。 我會很感激的!

Imports System

Module Module1

Sub Main()

    RunCMDCom("start", "%userprofile%\desktop\AnyShortCut.lnk", False)

End Sub


Public Sub RunCMDCom(command As String, arguments As String, permanent As Boolean)
    Dim p As Process = New Process()
    Dim pi As ProcessStartInfo = New ProcessStartInfo()
    pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
    pi.FileName = "cmd.exe"
    p.StartInfo = pi
    p.Start()
End Sub

端模塊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM