Link to home
Start Free TrialLog in
Avatar of AUROS
AUROS

asked on

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.
Avatar of momonana
momonana
Flag of Egypt image

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))
        {
            System.IO.Directory.CreateDirectory(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);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }

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

Open in new window

Avatar of AUROS
AUROS

ASKER

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

In my class library I have the following code:
        Process.Start("SomeProgram.exe");
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.

Thanks.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of AUROS
AUROS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AUROS

ASKER

no solution found