?
Solved

C# class library using an external executable

Posted on 2011-09-15
6
Medium Priority
?
438 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:AUROS
6 Comments
 

Expert Comment

by:momonana
ID: 36542416
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

0
 

Author Comment

by:AUROS
ID: 36542503
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.
0
 

Expert Comment

by:haggard
ID: 36542818
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40
ID: 36543170
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.
0
 

Accepted Solution

by:
AUROS earned 0 total points
ID: 37171230
No solution
0
 

Author Closing Comment

by:AUROS
ID: 37194255
no solution found
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question