【问题标题】:how to launch a (.exe) file in C++ using a window form application?如何使用窗口窗体应用程序在 C++ 中启动(.exe)文件?
【发布时间】:2012-02-28 05:04:14
【问题描述】:

,例如,当用户单击 Pre sets 按钮时,它将启动另一个 (.exe) 文件,Bread board 按钮也是如此。

这是我一直在使用的代码

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 部分的程序文件中。

【问题讨论】:

  • 您应该确保该文件存在。编辑:一个更有用的评论是确保 windows PATH 变量设置正确,或者使用完全限定的路径。
  • 我在解决方案资源管理器的引用中添加了 .exe 文件,但它会给出同样的错误

标签: .net visual-c++ c++-cli base-class-library


【解决方案1】:
Process::Start("C:\\application_directory\\PreSets.exe");

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

【讨论】:

  • 您确实将 C:\application_directory 更改为文件的实际路径。先检查文件是否存在
  • 我的意思是通过程序检查文件是否存在。还要给我文件和您的应用程序的确切路径
  • 正确的路径是“C:\\application_directory\\PreSets.exe”
  • 是的,我确实将其更改为实际路径,但仍然是相同的错误
  • 试试 dreta 的变体 C:\\application_directory\\PreSets.exe
【解决方案2】:

添加

  1. using namespace System::Diagnostics; 在给定的默认命名空间中。

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

【讨论】:

    【解决方案3】:

    您还可以使用 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();
             }
          }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-08
        • 1970-01-01
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多