简体   繁体   English

C#从另一个程序启动程序

[英]C# Starting a program from another program

I have this method 我有这个方法

private void StartSDCBackupSet() {
    using (Process p = new Process()) {
        p.StartInfo.FileName = "SDCBackup";
        try { 
            p.Start(); 
            BackIcon.ShowBalloonTip(5000, "Backup", "Editor for settings startet", ToolTipIcon.Info); 
        } catch (Exception) { 
            MessageBox.Show("Program til settings blev ikke fundet"); 
        }
    }
}

According to the documentation I can find - and quite a lot of answers here -, it should start another program - SDCBackup.exe. 根据我能找到的文档 - 这里有很多答案 - 它应该启动另一个程序 - SDCBackup.exe。

It doesn't. 它没有。 And I HAVE tried adding the .exe to the filename, but it does not make a difference. 我已经尝试将.exe添加到文件名中,但它并没有什么区别。 I get the messagebox with the message... I have checked p and StartInfo and everything looks right. 我收到消息的消息框...我检查了pStartInfo ,一切看起来都正确。 StartInfo.UseShellExecute is true. StartInfo.UseShellExecute为true。

Debugging reveals, that it is the line p.Start(); 调试显示,它是行p.Start(); that produces the exception. 产生异常。 And the Exception is a System.ComponentModel.Win32Exception with message: 而Exception是一个带有消息的System.ComponentModel.Win32Exception

File not found 文件未找到

Documentation says, that the code above does the excat same thing as Run in Windows menu, but it obviously does not. 文档说,上面的代码与Windows菜单中的Run一样,但显然没有。

If I write SDCBackup in the RUN section of Windows menu, SDCBackup.exe is started as it should. 如果我在Windows菜单的RUN部分中编写SDCBackup ,则SDCBackup.exe将按预期启动。 (And SDCBackup.exe is a ClickOnce installation, that nobody really knows where to find - other than Windows itself...) (而且SDCBackup.exe是一个ClickOnce安装,没有人真正知道在哪里找到 - 除了Windows本身......)

So why does my code not do the trick? 那么为什么我的代码不能解决问题呢?

Use 采用

p.StartInfo.FileName = "SDCBackup.exe";

Windows tests out known file types, and if it's an .exe (or any other known type, such as .bat, msc, scr, etc.) it runs it. Windows测试已知的文件类型,如果它是.exe(或任何其他已知类型,如.bat,msc,scr等),则运行它。 As far as I know .NET does not do that. 据我所知.NET不会这样做。

You could try using Path.GetFullPath ( https://msdn.microsoft.com/de-de/library/system.io.path.getfullpath(v=vs.110).aspx ) but I think it should work with relative paths as well. 您可以尝试使用Path.GetFullPathhttps://msdn.microsoft.com/de-de/library/system.io.path.getfullpath ( v= Path.GetFullPath ) Path.GetFullPath )但我认为它应该适用于相对路径同样。

Also make sure the file is inside your debug/release folder (whatever folder your .exe that is starting SDCBackup is in), since Windows looks in known directories that are contained in the PATH variable (such as C:\\windows\\System32 , etc), and .NET does not. 还要确保该文件位于debug / release文件夹中(启动SDCBackup的.exe所在的文件夹),因为Windows查找PATH变量中包含的已知目录(例如C:\\windows\\System32等) ),而.NET则没有。 If it is not in that folder, provide a fully qualified path and it should work. 如果它不在该文件夹中,请提供完全限定的路径,它应该可以工作。

It seems no one knows how to reference a ClickOnce installed application in another application. 似乎没有人知道如何在另一个应用程序中引用ClickOnce安装的应用程序。 (Asked the question at Microsoft, and got no useful response there either.) (在微软问这个问题时,也没有得到任何有用的回复。)

It seems ClickOnce apps are installed somewhere in the current users area, eg: 似乎ClickOnce应用程序安装在当前用户区域的某个位置,例如:

C:\Users\[user]\AppData\Local\Apps\2.0 

and from there 3 randomly named folders down - and not the same folder names for different users; 从那里下来3个随机命名的文件夹 - 不同用户的文件夹名称不同; so there seem to be no way to solve this. 所以似乎没有办法解决这个问题。

But I did get a way to create a regular installer from Microsoft, for Visual Studio C# solutions. 但我确实找到了一种方法来为Microsoft创建一个常规安装程序,用于Visual Studio C#解决方案。

I used it to set the install path of my apps, so they can find it each other - and it functions. 我用它来设置我的应用程序的安装路径,这样他们就可以互相找到它 - 并且它起作用。

It involves adding a Installer project to the solution. 它涉及向解决方案添加安装程序项目。

I found, that in addition to the explanation, 我发现,除了解释,

  • you have to go to Options for the installer-project, 你必须转到安装程序项目的选项
  • select **"Configuration Manager", 选择**“配置管理器”,
  • and set it to be included when building 并将其设置为在构建时包含

I also found, that version control is not automatic. 我还发现,版本控制不是自动的。 You must manually set version number - and for the sake of simplicity, this is a different format than the one Visual Studio uses. 您必须手动设置版本号 - 为简单起见,这是一种与Visual Studio使用的格式不同的格式。 If you do set the version, it will automatically uninstall previous versions of the same app. 如果您设置了版本,它将自动卸载同一应用程序的先前版本。

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

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