• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

C# class library using an external executable

Hi all,

I have a C# class library that uses an external executable (launching it through Process.Start).
I want the executable to be automatically copied to the bin folder of any application using this library, in the same manner that any DLL used by my library is copied to the using applications' bin folder, (without the need of adding a post build script to each of these applications).

The executable is currently part of the class library project, and is defined as "Resource" and "Copy if Newer".
I'm using VS2008.

Any idea if and how this can be accomplished.

Thanks in advance.
1 Solution
hoperful this code that help u

u can change the files path
or use the current directory of the class library to detect where is the
executable file exist and the bin folder

mohamed Naguib
// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:\Users\Public\TestFolder
// C:\Users\Public\TestFolder\test.txt
// C:\Users\Public\TestFolder\SubDir\test.txt
public class SimpleFileCopy
    static void Main()
        string fileName = "test.txt";
        string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";

        // Use Path class to manipulate file and directory paths.
        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);

        // To copy all the files in one directory to another directory.
        // Get the files in the source folder. (To recursively iterate through
        // all subfolders under the current directory, see
        // "How to: Iterate Through a Directory Tree.")
        // Note: Check for target path was performed previously
        //       in this code example.
        if (System.IO.Directory.Exists(sourcePath))
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            Console.WriteLine("Source path does not exist!");

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");

Open in new window

AUROSAuthor Commented:
That's not exactly what I meant...
I'll explain:

In my class library I have the following code:
Now in order for this to work, I need each program that adds a reference to my DLL to add the following to the post build event of their application:
        xcopy "$(SolutionDir)\..\Common\MyClassLibrary\bin\debug\SomeProgram.exe" "$(TargetDir)" /d /s /i /r /y

What I'm looking for is some workaround so that I won't have to add the post-build event for each application.

As I understand your problem, you want to include an external executable as part of the build project of an assembly and you want this executable to end up in the executable directory of any executable solution that includes your auxilliary assembly library.

Here's why your previous solution is failing: At the time that the library assembly builds it determines that it needs to copy the external exe to the output directory so it does. However, this output build directory belongs to the library assembly, not the executable solution that is including the library assembly. When the executable is built it copies the library assembly but knows nothing about the external executable.

There are two build time solutions:
1) Change the output directory of the library assembly to be the same as the one used by solution's executable.
2) Add the external executable to the solution's executable.
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

Jacques Bourgeois (James Burger)PresidentCommented:
You can reference an .exe the same way that you reference a .dll, through the Browse tab of the reference window.

Simply do that an then make sure that the Copy Local property of the reference is set to True.
AUROSAuthor Commented:
No solution
AUROSAuthor Commented:
no solution found
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now