Joseline Riker
Joseline Riker

Reputation: 25

How to include an external EXE with my code?

I have a third party program I use that what it does nothing else exists that does this so loading another library or NuGet package directly into Visual Studio 2019 wouldn't be an option. No DLL's for it or anything. It includes an exe I call with some parameters. Doing this with:

System.Diagnostics.Process videoprocess = new System.Diagnostics.Process();  
  System.Diagnostics.ProcessStartInfo videostartInfo = new  
  System.Diagnostics.ProcessStartInfo  
  {  
      WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,  
      FileName = "cmd.exe",  

So after this I call the program and arguments and it does it's thing and I just get back a thumbs up or down. When I'm done writing this program I will set it up on another computer. Is there any way to bake in the exe to the program so I can just call the exe without worrying about pathing or making sure I bring it with me to the final computer this code will run on all the time?

Upvotes: 0

Views: 1647

Answers (1)

sommmen
sommmen

Reputation: 7618

Normally the thirdparty executable is already installed on the system, else the exe is included in the output.

In the first case you could simply hardcode a path to Environment.SpecialFolder.ProgramFiles or Environment.SpecialFolder.ProgramFilesX86 (32bit), or really any path you choose.

A company where i work for has control over all systems that run our software and our hierarchy is always as follows:

  • Company
    • Software
      • App
    • ThirdParty
      • App1
      • App2

This makes it easy to locate thirdparty software.

Another option is to just include it in the build, so it gets output to the build directory. You can do this by right clicking your project and then doing 'add existing file' and then choose any extension in the file selector popup window.

The exe then gets added as content, and you can set it to 'copy if newer' in the properties tab: enter image description here

The csproj file wil look like so:

    <Content Include="HmiLogViewer.exe">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>

You could also try add as link, which will add the file as link:

enter image description here

    <Content Include="..\HmiLogViewer\HmiLogViewer\bin\Debug\HmiLogViewer.exe">
      <Link>HmiLogViewer.exe</Link>
    </Content>

If you want to add a directory of files, you can also add this piece of xml to the csproj:

  <ItemGroup>
    <DistributedFilesFolderFiles Include="..\..\DistributedFiles\**\*.*" />
  </ItemGroup>

  <ItemGroup>
    <None Include="@(DistributedFilesFolderFiles)">
      <Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Visible>True</Visible>
    </None>
  </ItemGroup>

Ofcourse adjust the path(s) to your situation.

Edit:

"That said do I need to pass the launch folder of the app as part of calling that exe file or will it automatically look there."

We look for it automatically with the option to override the value from a settings file (you could use Environment.GetCommandLineArgs), for example when we need to test an updated version of the thirdparty exe or when the path has changed.

public static string ExecutablePathAndFileName { get; } = Process.GetCurrentProcess().MainModule.FileName;
public static string DataDrive { get; } = Path.GetPathRoot(ExecutablePathAndFileName);
public static string ExecutableFolder { get; } = Path.GetDirectoryName(ExecutablePathAndFileName);

Upvotes: 1

Related Questions