2009-07-23 16 views

Antwort

21

Application.ExecutablePath

System.Windows.Forms.Clipboard

System.Media. *

Application.Exit

+2

Diese sind wahrscheinlich richtig für eine Windows Forms App –

2

Dies kann nicht genau das, was Sie suchen, aber nur für den Fall, dass Sie eine Verknüpfung verwenden möchten, wenn Sie einen Verweis auf die Microsoft.VisualBasic-Assembly hinzufügen, können Sie die raffinierten Tools VB-Programmierer haben Zugriff über die MyServices Namespace.

+0

gute Abkürzung, aber ja, ich suchte nach universellen Alternativen. Ich bin sicher, dass dies jemandem helfen wird. –

3

Wenn Sie eine WPF-Anwendung konvertieren, können Sie mit dem folgende:

System.Reflection.Assembly.GetExecutingAssembly().Location; 
//gets file path with file name 

System.Windows.Clipboard; 

System.Media.SystemSounds.[Sound].Play(); 

System.Windows.Application.Current.Shutdown(); 
0

Ich denke, dass Sie Meer rch ist dieser Satz:

Application.StartupPath; 
//Get file path without file name. 
3
My.Application.Info.DirectoryPath 
    AppDomain.CurrentDomain.BaseDirectory 

My.Computer.Clipboard 
    System.Windows.Clipboard //(WPF) 
    System.Windows.Forms.Clipboard //(WinForms) 

My.Computer.Audio.PlaySystemSound() 
    System.Media.SystemSounds.*.Play() 

My.Application.Shutdown() 
    System.Windows.Forms.Application.Exit() //(WinForms) 
    or 
    System.Windows.Application.Current.Shutdown() //(WPF) 
    or 
    System.Environment.Exit(ExitCode) //(Both WinForms & WPF) 
+0

Diese My.Application.Info.DirectoryPath AppDomain.CurrentDomain.BaseDirectory gab verschiedene Antworten für mich. Die richtige Antwort scheint GetFilePath zu sein (System.Reflection.Assembly.GetExecutingAssembly.Location) – user2728841

2

Von decompiling Microsoft.VisualBasic.dll, dem eigentlichen Code, wenn My.Application.Info.DirectoryPath ist Aufruf ausgeführt wird:

Path.GetDirectoryName(
    new AssemblyInfo(
     Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).Location); 
1
System.IO.Directory.GetParent(Application.ExecutablePath) 

ist genau das gleiche wie:

My.Application.Info.DirectoryPath 

Wenn Sie nur tun:

Application.ExecutablePath 

Sie erhalten die ausführende Datei an den Pfad angehängt, die überhaupt nicht nützlich sein kann.

+0

Laut [Scuzzlebutt] (http://stackoverflow.com/a/36102900/1677912) ist dies nicht korrekt. Die GetParent-Methode gibt ein DirectoryInfo-Objekt und keinen Pfad (Zeichenfolge) zurück. Sie müssen die FullName-Eigenschaft verwenden, um den Pfad tatsächlich abzurufen. Verwenden Sie anstelle von System.IO.Directory.GetParent (Application.ExecutablePath) 'System.IO.Directory.GetParent (Application.ExecutablePath) .FullName'. – Mogsdad