Come posso avviare un'applicazione usando C #?
Requisiti: deve funzionare su Windows XP e Windows Vista .
Ho visto un campione dal campionatore DinnerNow.net che funziona solo in Windows Vista.
Come posso avviare un'applicazione usando C #?
Requisiti: deve funzionare su Windows XP e Windows Vista .
Ho visto un campione dal campionatore DinnerNow.net che funziona solo in Windows Vista.
Risposte:
Usa il System.Diagnostics.Process.Start()metodo
Dai un'occhiata a questo articolo su come usarlo.
Process.Start("notepad", "readme.txt");
string winpath = Environment.GetEnvironmentVariable("windir");
string path = System.IO.Path.GetDirectoryName(
System.Windows.Forms.Application.ExecutablePath);
Process.Start(winpath + @"\Microsoft.NET\Framework\v1.0.3705\Installutil.exe",
path + "\\MyService.exe");
Ecco uno snippet di codice utile:
using System.Diagnostics;
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = arguments;
// Enter the executable to run, including the complete path
start.FileName = ExeName;
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
int exitCode;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
}
C'è molto di più che puoi fare con questi oggetti, dovresti leggere la documentazione: ProcessStartInfo , Process .
PathTo*.exema non mi aspetto che funzioni. (a) cosa succede se ci sono più corrispondenze? (b) Spero che il codice di Microsoft non lo consenta, poiché sarebbe una sicurezza debole.
System.Diagnostics.Process.Start("PathToExe.exe");
Se hai problemi con System.Diagnostics come ho fatto io, usa il seguente semplice codice che funzionerà senza di esso:
Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.StartInfo.Arguments = "mytextfile.txt";
notePad.Start();
Processè in System.Diagnostics.
Inoltre, vorrai utilizzare le variabili di ambiente per i tuoi percorsi, se possibile: http://en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows
PER ESEMPIO
Ci sono molti altri controlla il link per un elenco più lungo.
Metti il tuo file.exe nella cartella \ bin \ Debug e usa:
Process.Start("File.exe");
Utilizzare Process.Start per avviare un processo.
using System.Diagnostics;
class Program
{
static void Main()
{
//
// your code
//
Process.Start("C:\\process.exe");
}
}
Prova questo:
Process.Start("Location Of File.exe");
(Assicurati di utilizzare la libreria System.Diagnostics)