Add files to existing project programatically using C# - c#

I am currently developing a solution that has two projects on it: one is some kind of API where I put all the functions that I want to use in the second.
So, at some point in the project, I need to create a file programatically. This is ok, but I need to include that file in my second project to use the functions from the first projects! Any idea on how to do this?
I have tried the EnvDTE but I think it only works if you are creating a new solution and a new project.
Really need help on this! Thanks in advance! :)

Are you literally trying to recompile the second project after creating these files or is this all happening at runtime?
To me it sounds like what you're trying to do is really not create a file but to emit a class in memory during runtime to make new functions available to the second executable. You should check out Reflection.Emit. That is how you define classes, even assemblies, that didn't exist when you started running the program. Check out MSDN and this code project article.

Related

How can I embed other applications in asp.net?

I have created three projects on Visual Studio. One is the base project and I would like to embed the other two projects into the base project. I went through some articles but couldn't find something that suits my requirements (and I am still a newbie in asp.net :)). Below is a screenshot of what I created
I will like to call FirstApp and SecondApp when I run the BaseApp and also display some unique texts like "hello from FirstApp" and "hello from SecondApp".
All you need to do is create a reference to the projects you want to use, and then call the code in those projects.
You can add a reference to another project by right clicking BaseApp, select Add, and Reference. Then you get a screen where you can select the other projects in your solution. Select the ones you want to use, and you can start to use the classes in the other projects.
If you want to use FirstApp and SecondApp then create class library of those project and add those library reference in your first project [base].
After that you will get all method access in this project based on assembly type.
If what you want is to see on a page loaded from a web application another page loaded from a different web application, then you need to use iframes.
To do this you do not need even if the projects are in the same solution. They are different processes. They could even be in different domains. You are really using the http protocol to create the iframes. The applications are completely isolated.

Can you call a class in a visual studio website project from a class library?

I'm creating a new VS2015 web application, but there's one piece that requires some reporting that already exists in another system.
The other system is a VS2013 solution that has a website (not web application) as it's main project, along with a number of class libraries. But the website directly contains a bunch of classes that do reporting and other things, and I would rather use those than recreate all the (very complicated) code.
Is there any way to reference the classes in the website from another project or class library? It's a rather large old application I'm maintaining, and I'd rather not try extracting all that functionality into it's own class library if I don't have to.
If those classes were already in a separate class library, I could reference them easy enough, but unfortunately they are right in the website, and I can't find any information about being able to link to it (presumably because you can't).
Here's a sample structure:
MySolution
MyNewClassLibrary
MyClass
{
MyReportFunction()
{
var x = new ReportClass(); // From website project
x.CreateReportFunction();
}
}
OldSolution
WebsiteProject
ReportClass
{
CreateReportFunction()
{
// All the code I'd like to access
]
}
Is this possible? Or do I have a lot of rewriting to do? Or would looking into converting the website into a web application be a better idea?
You can add those existing classes as linked classes into your solution.
To add an existing item to a project
In Solution Explorer, select a target project.
On the Project menu, select Add Existing Item.
In the Add Existing Item dialog box, locate and select the project item you want to add.
From the Open button drop-down list, select Add As Link.
You can also read more details here.
In case anybody else is looking for something like this, the answer is no, it's not possible.
I ended up pulling all the functionality out into a new class library project, which required massive testing to make sure I got all the little bits and pieces right.
Moral of the story - build your software properly the first time, and pull functionality out into reusable objects. Don't do procedural programming in an object-oriented language.

Smart DLL selection thru code

I am currently working on a project that has an application layer and class library which is working great.
But I got a new requirement for another client for the same application everything remains same except the logic in class library.
So, I am wondering if we can have some solution to have two different class libraries for each application and based on the appsetting in web.config, we can select which library to load.
Any help and ideas are greatly appreciated.
Thanks.
Have you tried looking at MEF from microsoft? http://en.wikipedia.org/wiki/Managed_Extensibility_Framework it allows you to add plugins at runtime, which means you could pick which dll you wanted to load.

Possible to inject resource files into .net assembly?

There's a .net program for students that I've been messing around with in Reflector and Reflexil, but it doesn't look like they can work with resource files. I want to replace the default background with a custom one. How feasible is this?
I've tried exporting the assembly as a C# project and maybe rebuilding with the resource files swapped, but a few classes won't decompile right when exporting and crashes Reflector when I try to view the full source code inside reflector (after clicking 'expand methods')
What type of applicaiton is it? WinForms, WPF, Silverlight, Web? You can do it with ILDasm round triping. If you let me know what type of app it is I'll post the steps you need to take.
Reflexil 1.3 is now able to handle resources.
Quite difficult, probably Hex Editor can help.
Resource Hacker can do it. Follow; Action > Add a new Resource
delphi.icm.edu.pl/ftp/tools/ResHack.zip
I just put out GrayWolf to edit programs, I don't have the ability to edit resource(s) (if you donate$:) yet
but I would think the easy change is to edit the program to load the background from disk (./back1.jpg)
I have others tools that would make it easy to change the background of the running program see
"Hacking .NET" at DefCon18
other tools "GrayDragon"
I have a payload that does just this, it is for DefCon19, but if you send me an e-mail...
Happy hunting

Storing Images to use in application

I would like to store some images to use in my C# application. They are png files and are currently in a folder with the dlls. Ideally I would like to have them included with the dll so i dont have to include the actual images with the installation.
What is the best way to do this?
I have though about resx but i am unsure as to the best way to go about it. Should I use create the resx file using another project, and then add it to the one I want to use it with?
Thanks in advance.
The easiest is to just add them to your current project and then set their Build Action property to Embedded. I believe that automatically adds them to a resource file and then you can access them using reflection.
Here's an article on retrieving them:
MSDN
Put them in a resource file. You can add it to the same project, no need to create another one.

Categories