Create new c# project files from code - c#

I'm currently working on an application that will generate actual .cs and .xaml code files and add them to a project. I've managed to do this by manually editing an existing .csproj file and thats working well.
However I would like to be able to create the project files from my application as well, to cut out the extra step of creating the project first then running the application after.
Does anyone know how to create a C# project (class library, or WPF Application) from an application? I've looked into DTE, but I've hit a wall

I can't give a complete answer, but maybe I can point you in the right direction.
One avenue to explore in your own searching is MSBuild. C# and VB.net project files follow the msbuild format, and so the first step in building a valid project file is building a valid msbuild file.
Also, it sounds like you're doing something a little different, but have you looked at the T4 system for code generation?

Related

How do I link a source file between two proejcts in Visual Studio Code or dotnet command line?

I have some common code between two project solutions, and I would like to link a source file from one solution to another. Specifically, I would like to add/link a single .cs file from a different solution to my current one.
This seems to be a good answer. However, Visual Studio Code does not have the add as link or add existing item. It only have Add Folder to Workplace. Furthermore,dotnet add and dotnet sln add only takes .csproj file, which is the whole project.
So how do I achieve this?
We generally don't share sources files because it creates all sorts of problems.
What we do is put the code in a class library and share the project (or assembly) with anything that needs it

Build same Project as Console and DLL

I've got an C# Project in Visual Studio, which has Console Application as Output Type.
But I also need a Class Library of this project for another solution.
Right now I have to switch the output type every time, but I wonder if it's possible to generate exe and dll at the same build-event?
Is there a post-build-event for this?
To my knowledge there is no possibility to change the output type after compilation. That being said, if would be possible to have two projects like Console and Library in your solution, which would use the same source code files but have different output types. That way you would have different outputs without any duplication of code.
it is generally possible to reference a .net exe assembly as it would be a class-library.
So you can just stick in creating an exe file and reference the exe (sounds strange, but works) in your other project.
This is the dialog for browsing for references. As you see you can select exe files.
But as commented it really depends on what your usecase is.
I don't recommend to ship an exe with an entry point to your customer hoping that the customer does not discover the exe. But what you could do about that is to conditionaly compile your entry point.
For example
class Program {
// This is the entry point of the EXE
public static void Main() {
#if DEBUG
// Start Debug Application
...
#else
// Shipped to client - Entry point disabled
return;
#endif
}
}
If there is a relevant reason to have a shipped exe and a shipped class library, I would refactor your solution like this:
(A) complete application (.sln)
(B) console-application (.csproj) which has a reference to (C)
(C) class library project (.csproj)
With that it is perfectly clear to others that there is an application that uses the library and the library itself.
Console Application is the type of your project. You can not change it.
What you can -and must- do is, carry your logic into a Class Library project and use your class library from any type of project you want.
You should compile your project to become a dll and then use this dll in a console application.
A possibility to achieve what you want is to manually run the msbuild on your post-build event of your project.
See: How do i build a solution programatically in C#?
or Building C# Solution in Release mode using MsBuild.exe
The usual solution for this is using a Solution with two projects:
a Class Library with all the code (which builds into a DLL)
an Console Application referencing the library whose Main just calls some function(s).
For more information, check the MSDN page on Solutions.
Codor suggested manually adding the files to the Console project, but one downside is that build settings are not shared between both versions, so you might get some inconsistency there.
I'm not really sure why people think it's not possible but it actually is.
The easiest way would be renaming the exe to dll Sounds stupid, I know. But it works in many cases. Also, as "Peter I" said a .NET exe can be imported as assembly in other projects. So you might not actually need a dll anyways.
Another way would be using C# command line as stated here: /out (C# Compiler Options)
You can use command command line options in Pre/Post build events Pre-build Event/Post-build Event Command Line Dialog Box
I have a similar requirement and couldn't find any definite answer in this post or anywhere. I currently have a class library and would like to create a console application project without copying any code. Ideally speaking there should be two projects, one for creating a console application and another for creating a class library. And this is what the visual studio also suggests. When I tried to run the class library, I got the below message.
It clearly asks us to add an executable project to the solution and add the reference to the library project.
Below are the steps to do this.
Right click solution -> Add new project -> Console App -> choose name -> ok.
Right click on the console project -> add reference -> In reference manager, click on the projects tab and select the other project(In my case this is the class library project, In case it is not listed just click on browse and select the .csproj file) -> ok.
Now to use the classes in the other project, simple do using LibraryProjectNameSpace
There we are. Bingo!!!!
Also as mentioned in the other answers it is not possible to have the same project generate both .exe and .dll. But you can have the same solution generate these two guys by having two projects. In this way there is no need to switch the output of the project every time.
FYI, I use visual studio 2017

Adding a VB file into a C# project

I am currently trying to add a VB file inside a C# project, but I am only able to add C# files. Is there a way for me to be able to add more language templates like what is shown below?
This picture below is an example of what I have been able to do in an old project which is to be able to add both VB and C# files into one project.
Update: I am redeveloping an ASP.NET 2.0 site to the newest version with the new bootstrap framework. I didn't realize the old project was using mostly VB until I started the redevelopment in C#. I noticed that the old project is also using some C#. I am trying to see if I can have those two languages inside a new ASP.NET project or not by adding VB files. If not, I'll just make a VB project and convert all of the C# code to it.
When you open the "Add new file" dialog inside a project, it is filtered by the current project type you're working on. E.g. you cannot add a *.vb file to a C# project type, without hacking it somehow.
The GUID within the *.csproj file defines the current project type. This site contains a set of known GUIDs, it is somewhat outdated since it's from 2008.
As I said in a comment, the reason you have been able to mix-in both VB and C# code within the web project, is by using CodeFile attribute, rather than the CodeBehind attribute. The latter will compile all the source files within the project into an assembly with the same name. This will be the file you are uplading to your webserver along with the .aspx files.
Sample file structure:
bin/MyApp.MyProject.dll
Index.aspx
The CodeFile variant, which compiles the source on the fly will need all the files in the same directory, or in the directory specified within the attribute, for this example it will reside within the same directory. This will also allow you to change the code at the web server, and not having to download the code to your development environment. (This is NOT recommended, as you wave goodbye to any version control and other useful tools.)
Sample file structure:
Index.aspx
Index.aspx.cs
My advice would be to decide whether or not to continue developing in VB, or switch to C#. Containing your codebase to one language is preferrable. And if you decide to refactor the whole solution, why not give ASP.NET MVC a go? :-)
Actually there used to be, not sure if it's still available, to put a VB file in a C# project. You would have needed to add a text file type but name it with a .vb extension. Then place it in a folder. Then in the app or web config, you needed to add some config to tell the runtime where to find the vb files and to use VB compiler to compile the code.
It worked but was clunky and not recommended. Again, not sure if it's available anymore.
Personally, if you have VB code, use a vb2cs converter like this one to begin converting it over. They are not perfect but it's at least a good start in most cases.

Getting C# to recognize a dll outside of Visual Studio

Several years before I started working at this job another developer who is no longer here wrote an application in classic ASP using HTML, vbscript and javascript. This is fine but the problem is that 2 pages were written in C# with an HTML file and a code behind file. There was no solution files for these two pages. They may have been originally created in Visual Studio but they don't exist in it now.
That is important because there is a lot of things that Visual Studio just does for you without even thinking.
My problem is that in these two C# pages I need to get them to reference a DLL. This is a simple task when using Visual Studio. You just add a reference to the project and life is good. But outside of VS nothing seems to work.
I tried putting the dll in the same folder as the pages and then I tried the following:
Using myDLL;
myDLL dll = new myDLL();
myDLL dll = myDLL();
I found some code online that said to create an internal static class and use [DLLImport()] but that didn't work either. It couldn't find the dll or the Entry Point for the dll. I am currently researching how to create an entry point, just in case this is the method to make everything work.
Outside of having to rewrite these pages in vbscript (which I don't have the time to do) I am at a loss.
Has anyone ran into this problem before? Is there something that I can put in the web.Config? Or is this just impossible and I am hosed.
BTW this is all running under the 2.0 .net framework.
If you drop the DLL you want the code to reference into the bin folder of the website, then open the web.config and locate the following section configuration -> system.web -> compilation -> assemblies.
You need to add the display name of the assembly to that list - so that the compiler will reference that assembly during it's late-bound build process.
Now you should be able to use the stuff that's in it on those pages.
If you don't the know the display name of the assembly (typically yourassembly, version=*.*.*.*, Culture=neutral, PublicKeyToken=null for culture-invariant, non-strong-named assemblies) you can open it in a tool like ILSpy (there are others, it's just become my favourite) and it tells you when you select it in it's UI:
sorry for the poor highlighting - jerky hand following far too much coffee
If all the code in that assembly is in a single namespace, also, you can also add a default using to all the .cs or .aspx code in the project by adding that namespace to configuration -> system.web -> pages -> namespaces - making it simpler to use that code in the pages.
I created a VS Solution/Project for my app. I compiled and published it to the web server. When I published it I had it copy all project files.
I ran it and it crashed because it could not find my dll.
I tried adding the lines that Andras mentioned above and it seemed like it was getting me closer but it only changed the errors I was getting.
Then I went into IIS on the web server. I expanded the folder listing under Web Site. I right clicked on the folder that contained my app and made that folder into an application folder.
After I did that everything just worked. So then I thought I would see what happened if I backed out all of the additional code I added to my C# app and the Web.Config file. It still worked. All I needed to do was to make the folder an application folder in IIS and put a Using statement in my C# app and life is wonderful again.
Thanks for all the comments and suggestion. Andras thanks for the link to ILSpy. That is a cool little tool.
Take care,
Robert
I agree with Jon, it sounds like you should try creating a new project for these files. It's always better to leave code better off than you found it. If a new project is not an option for some reason, you should indicate this in your question.

Custom build step for C# project

In C++ projects there is the possibility to set a custom build step for files. Is there a similar functionality in C# projects? I couldn't really find anything.
One idea would be to create a second project (makefile or c++) and move the files there.
MsBuild should work for you although it might take some time to figure out how it works. It appears that you can setup a step that runs prior to building each .cs file by separating each .cs file into its own build group.
In MSBuild script for compiling each .cs file into an EXE, Dino Chiesa comments:
By using the %(CSFile.identity)
scalar, we run this task once for each
file. The converse would be
#(CSFile.identity). That would run
the compile once, for all files,
compiling them all together into a
single assembly.
Also, these links might help:
Custom build step for C# files
Master Complex Builds with MSBuild
No custom build step for individual files with C# projects. You could probably hack something together with MSBuild...
Look at the BeforeBuild and AfterBuild targets in your csproj file.
I think you are on the right track with your comment about multiple projects. Combine this with the fact that you can include multiple projects within a single Solution and you may have your answer. I use this functionality to build several components at a time and it works quite well.

Categories

Resources