Solved

C# class library using an external executable

Posted on 2011-09-15
6
422 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
No solution
0
 

Author Closing Comment

by:AUROS
Comment Utility
no solution found
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now