如何使用窗体窗体应用程序在C++中启动(.exe)文件?

问题描述:

,当用户点击预设按钮时,它会启动另一个(.exe)文件,对于面包板按钮也是如此。如何使用窗体窗体应用程序在C++中启动(.exe)文件?

这是我一直在使用

namespace RC_lab { 
    using namespace System; 
    using namespace System::ComponentMode1;   
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 
    using namespace System::Diagnostics; 

为按钮它就像

Process::Start("PreSets.exe"); 

的代码,但它给了我一个错误,这个代码适用于

Process::Start("notepad.exe"); 
Process::Start("chrome.exe"); 

它会正确地启动它们,但在我的情况下,我得到:

Win32Exception was unhandeld系统找不到指定的文件。

我确定文件存在,甚至将文件夹放在C分区的程序文件中。

+2

你应该确保文件存在。编辑:更有用的评论将是要确保Windows PATH变量设置正确,或使用完全合格的路径。 – Matthew 2012-02-06 18:08:09

+0

我在解决方案资源管理器中的refrences中添加了.exe文件,但它会给出相同的错误 – 2012-02-06 18:10:41

Process::Start("C:\\application_directory\\PreSets.exe"); 

您还必须指定文件的位置。

+0

我这样做了,但它仍然给出相同的错误 – 2012-02-06 18:15:57

+0

您确实更改了C:\ application_directory文件的实际路径。检查文件是否先存在 – 2012-02-06 18:18:38

+0

我的意思是以编程方式检查文件是否存在。也给我的文件和您的应用程序的确切路径 – 2012-02-06 18:18:51

只是在给定的默认名称空间中添加

  1. using namespace System::Diagnostics;

  2. 并将Process::Start("chrome.exe");添加到按钮中。

您还可以使用OpenFileDialog启动exe文件或任何其他文件。 见下面

 // Displays an OpenFileDialog so the user can select a Cursor. 
    Stream^ myStream; 
     OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog; 


     openFileDialog1->FilterIndex = 2; 
     openFileDialog1->RestoreDirectory = true; 

     if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) 
     { 
     if ((myStream = openFileDialog1->OpenFile()) != nullptr) 
     { 

      String^ strfilename = openFileDialog1->InitialDirectory + openFileDialog1->FileName; 


       Process::Start(strfilename); 


      myStream->Close(); 
     } 
     } 

代码的最好方法是将所有的“\”为“/”。当我看到我的项目的警告时,我发现了这一点。 例如c:Desktop \ ex.exe到C:/Desktop/ex.exe。