简体   繁体   中英

How to copy non-code files with the compiled project when referenced by another project in C#

I have a project that acts as wrapper for a 3rd party .exe (it has static methods to create the command line and run the executable). I want to use it in at least a couple of projects in my solution. Ideally, the .exe should only be in that wrapper project (I don't want to have to add it to each project that uses it). Right now I'm trying to get this to work with a web project (.NET MVC) running on IIS 7 but when I use Assembly.GetExecutingAssembly().Location to see the directory that my wrapper is called from I'm in a folder like

C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\"my project"\\65a016fb\\ac5f20a7\\assembly\\dl3\\d8de0f10\\06e277a2_55b2cd01

and my 3rd party .exe is nowhere to be found. Is it possible to copy files with a reference that do not compile?

Btw, I have set the "Copy to output directory" and "Build Action" properties of my .exe to "Copy always" and "Content"/"Resource"/"Embedded Resource" without success so far.

Try to add an existing files in your projects which use your wrapper. When the dialog box appears, do this:

http://wiki.oxygenelanguage.com/en-w/images/0/0d/AddAsLinkAero.png

Either that or drag neccessary files to your projects from the wrapped with Alt holded.

That will add the files as links. Links are good choice for reusal whenever the original files has to be located in the only place.

Adding files as links in conjunction with setting to that links build action to Content and Copy always to the option of copying them to the output directory should work for your.

If anyone runs into this problem, here's how I got around it:

  • Add the .exe to your project's resources (right-click on the project, Properties -> Resources, add file and give it a resource name)
  • You may now access your .exe as a byte[] (at least if you're building to a dll) with Properties.Resources.*NameYouGaveYourResource*
  • Before using your .exe, use File.Exists() to check if the file exists in Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) (or you can put the exe wherever you want using the method, above gets you directory the .dll is in) - if not, write out the file:

using (FileStream fileStream = new FileStream(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\EXE.exe", FileMode.CreateNew)) { fileStream.Write(Properties.Resources. NameYouGaveYourResource , 0, > Properties.Resources. NameYouGaveYourResource .Length); }

Now you can use the exe easily as a Process . Pretty far from ideal, but it means the exe accompanies your .dll wherever it goes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM