2009-07-23 52 views

Trả lời

21

Application.ExecutablePath

System.Windows.Forms.Clipboard

System.Media. *

Application.Exit

+2

Đây có thể là chính xác cho Windows Forms App –

2

này có thể không chính xác những gì bạn đang tìm kiếm, nhưng chỉ trong trường hợp bạn muốn có một shortcut, nếu bạn thêm một tham chiếu đến Microsoft.VisualBasic assembly, bạn có thể sử dụng các công cụ tiện lợi VB lập trình viên có thể truy cập thông qua không gian tên MyServices.

+0

phím tắt tốt, nhưng có, tôi đang tìm kiếm các giải pháp thay thế phổ quát. Tôi chắc rằng điều này sẽ giúp một ai đó mặc dù. –

3

Nếu bạn đang chuyển đổi một ứng dụng WPF, bạn có thể sử dụng như sau:

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

Tôi nghĩ rằng bạn biển rch là câu này:

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

Những My.Application.Info.DirectoryPath AppDomain.CurrentDomain.BaseDirectory đã đưa ra các câu trả lời khác nhau cho tôi. Câu trả lời đúng có vẻ là GetFilePath (System.Reflection.Assembly.GetExecutingAssembly.Location) – user2728841

2

Từ dịch ngược Microsoft.VisualBasic.dll, mã thực tế đó được thực hiện khi gọi My.Application.Info.DirectoryPath là:

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

là chính xác giống như:

My.Application.Info.DirectoryPath 

Nếu bạn chỉ do:

Application.ExecutablePath 

Bạn sẽ nhận được tệp thi hành được nối vào đường dẫn, có thể không hữu ích chút nào.

+0

Theo [Scuzzlebutt] (http://stackoverflow.com/a/36102900/1677912), điều này không đúng. Phương thức GetParent trả về một đối tượng DirectoryInfo, không phải là một đường dẫn (chuỗi). Bạn cần sử dụng thuộc tính FullName để thực sự có được đường dẫn. Thay vì 'System.IO.Directory.GetParent (Application.ExecutablePath)', sử dụng 'System.IO.Directory.GetParent (Application.ExecutablePath) .FullName'. – Mogsdad

Các vấn đề liên quan