简体   繁体   English

如何使用WPF窗口从任何计算机上的桌面文件夹启动应用程序

[英]How To Use A WPF Window To Launch An Application From Desktop Folder On Any Machine

I keep searching the Internet for some sort of tutorial on this but none I've fount make it very clear. 我一直在互联网上搜索有关此问题的某种教程,但我没有找到一个非常清楚的例子。 I've created a WPF Window in Visual Studio that has two Buttons Button One Is to be used to launch a PDF File. 我已经在Visual Studio中创建了一个WPF窗口,该窗口具有两个按钮。一个按钮用于启动PDF文件。 Button Two is to be used to launch the .exe application and then close the WPF window Now I find that if I use: 按钮二将用于启动.exe应用程序,然后关闭WPF窗口。现在,我发现如果使用:

System.Diagnostics.Process.Start("C:/Users/mdkgr/Desktop/FolderName/app.exe")

Then it works, however, naturally when I publish this project out and people are to use it on their machine this method will not work because the address will no doubt be different for every machine it's installed on. 然后它起作用了,但是自然地,当我发布该项目并且人们要在他们的计算机上使用它时,此方法将不起作用,因为毫无疑问,其安装的每台计算机的地址都会不同。 My question is simply: How do I make this so it will launch either the PDF file or application.exe on any machine it's installed on? 我的问题很简单:我如何做到这一点,以便它将在安装了它的任何计算机上启动PDF文件或application.exe? None of the Visual Studio tutorial I've seen make this very clear. 我见过的Visual Studio教程都没有一个很清楚。

Edit: 编辑:

I tried @bwing ' s idea but I get an error so I'm not sure if I'm using it right?? 我尝试了@bwing的想法,但是遇到了一个错误,所以我不确定是否正确使用了它? Here is my code in the public voids: 这是我在公共场合中的代码:

`public static void LaunchKOS()
        {
            var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            // Launch Voice Server
            //Process.Start("C:/Users/mdkgr/Desktop/Knight O S Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer");

            var combinedPath = Path.Combine(desktopPath, "Knight O S Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer");
            Process.Start(combinedPath);

            // Launch Knight O.S. Unity Settings Window
            Process.Start("C:/Users/mdkgr/Desktop/Knight O S Beta01");
            // Close This Window
            Environment.Exit(0);
        }
        public static void LaunchPDF()
        {
            Process.Start("C:/Users/mdkgr/Desktop/Knight O S Beta01_Data/Knight O.S. Features.pdf");
        }`

EDIT: 编辑:

This method is working just fine on one of my machines but as soon as I try it on my HP laptop it only wants to open the Unity3d application. 此方法在我的一台计算机上运行良好,但是一旦在HP笔记本电脑上尝试该方法,它只想打开Unity3d应用程序。 and yet on both machines the folder directories are exactly the same and I can manually open each .exe files with no problems but my launcher application only wants to open the one .exe file on my HP laptop. 但是在两台计算机上,文件夹目录完全相同,我可以毫无问题地手动打开每个.exe文件,但是启动器应用程序只想在HP笔记本电脑上打开一个.exe文件。 Does anyone have any clue why this would be happening? 有谁知道为什么会这样吗?

 public static void LaunchKOS() { var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); var desktopPathkos = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Launch Voice Server var combinedPath = Path.Combine(desktopPath, "Knight OS Beta01_Data/voice-recognition-server-pc/VoiceServer/bin/Release/KittVoiceServer"); Process.Start(combinedPath); // Launch Knight OS Unity Settings Window var combinedPathkos = Path.Combine(desktopPathkos, "Knight OS Beta01"); Process.Start(combinedPathkos); // Close This Window Environment.Exit(0); } 

If the files are on the desktop, you can get the path to the desktop with 如果文件在桌面上,则可以通过以下方式获取到桌面的路径

var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

If the files are in the same folder as the WPF Window application you can use this to get that folder 如果文件与WPF Window应用程序位于同一文件夹中,则可以使用该文件夹来获取该文件夹

var exePath = AppDomain.CurrentDomain.BaseDirectory;

Then you just append the file name and make the call as you were before 然后,您只需追加文件名并像以前一样进行通话

var combinedPath = Path.Combine(desktopPath, "app.exe");
Process.Start(combinedPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM