Visual Studio 2019 Exports C# Program as DLL instead of EXE? - c#

I am very confused because I created a new project in Visual Studio 2019. I have tried this three separate times with different projects, and each time it exports as a DLL instead of an EXE. Here are the steps to reproduce this problem:
New project
Console App (.NET Core)
Set details for new project, and hit the "Create" button
Add Microsoft.Win32.Registry NuGet package to project
Add the following code:
using Microsoft.Win32;
using System;
namespace Key_Statistics_Startup_Changer {
class Program {
static void Main(string[] args) {
if (args[0] == "CREATE_STARTUP") {
RegistryKey rkey = Registry.CurrentUser.CreateSubKey(#"Software\Microsoft\Windows\CurrentVersion\Run");
rkey.SetValue("Key Statistics", #"C:\Program Files\Key Statistics\Key Statistics.exe");
}
else if (args[0] == "REMOVE_STARTUP") {
RegistryKey rkey = Registry.CurrentUser.CreateSubKey(#"Software\Microsoft\Windows\CurrentVersion\Run");
rkey.DeleteValue("Key Statistics");
}
}
}
}
Build and run project
When I do this, the program does create or remove the desired registry from startup (what the code is trying to accomplish), when I input arguments through Visual Studio. However, when this project is built, my Key Statistics Startup Changer\bin\Debug folder gives me a sub-directory netcoreapp2.1 with the following files:
Key Statistics Startup Changer.deps.json
Key Statistics Startup Changer.dll
Key Statistics Startup Changer.pdb
Key Statistics Startup Changer.runtimeconfig.dev.json
Key Statistics Startup Changer.runtimeconfig.json
I am positive that I have the right folder where it would export, and every time I re-create the steps (making sure I don't select C# DLL project), this happens.
What's the deal here?

You went wrong when you chose Console App (.NET Core) instead of Console App (.NET Framework) ,
In a nutshell, it's harder getting a .exe from .net core than .net framework.
No worries though, you can simply copy all your code and paste them in a new
Console App (.NET Framework) project.

Right click on the project and go to properties. Choose the Application tab and, on the right side, you have an option called Output Type. You can choose whatever you want; for example, if you want your project to emit a DLL, just choose Class Library.

When you are setting up the Application Folder: Add/Project Output/... on the options to choose, pick "Publish items" instead of "Primary Output".
This will package .exe files instead of .dll, if you do want .dll files, then choose "Primary Output" to the Output Group.

Related

Error : I am new to C# programming and rying to get started using VSCODE as an text editor

How do I solve this.
Couldn't find a project to run. Ensure a project exists in D:\c#, or pass the path to the project using --project
I have installed the .NET SDK in C drive and trying to run the program saved in D drive. I am using Visual Studio Code. And just got started with C#. This is the code I am trying to execute:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Let's get started from scratch. Follow these steps:
Open your Visual Studio Code and press Ctrl + `. This will open your Console.
Within your console, you will see a default path as C:\Users\ (something like this). Now write this command: mkdir CSharp.
This will create a folder named CSharp inside your C:\Users folder.
Next, type this command: cd CSharp. This is required to jump to the folder you just created in step 2.
In this step, write dotnet new console. This is the command to create a basic console application. Make sure you have installed .NET CLI on your system.
Write dotnet run. This will run your console application.
Firstly, you are missing a parenthesis } from the end of your code file.
Secondly, you didn't mention how you created this code file, but the error message suggests that you have simply added a new .cs file to a folder. Visual Studio Code doesn't work that way, and requires a project file to tell the compiler what to do with your code.
You can also run the C# compiler directly but given that you have Visual Studio Code and want to use it to manage your projects, the first option of using the project file as it is designed to do would probably be preferable.
Honestly there's a lot of missing information in your question, here what I suggest:
Go to a folder of your choice in the cmd run:
(Considering that you installed .net core cli)
dotnet new console.
Open the folder with VScode, run the project.

Visual Studio Code can't run C# file

I am starting a tutorial about C# and, unfortunely, my PC can't handle Visual Studio, so a friend of mine recommended me Visual Studio Code, because it's a lot more light. I installed it, I installed the .NET Core SDK and the C# extension powered by OmniSharp, and after that, I created a folder in my Desktop, then I created my "file.cs" in that folder (inside VS Code), and I had written a really simple "Hello World" program inside it. And when I tried to run it without debugging, it asked me to select an enviroment, and I selected ".NET Core". It created another folder inside my project folder called ".vscode" and inside that folder it created a file called "laucnh.json" that contains the following code:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
After that, an error message appears on the bottom right corner of the screen and says "Cannot create .NET debug configurations. The OmniSharp server is still initializing or has exited unexpectedly.", but I think that I installed everything correctly. Am I missing something?
You can not run a file in C#. Try create a new console application with
dotnet new console
A {projectName}.csproj and and Program.cs will appear. Put your code into the Main method of the Program class and it will probably work
https://learn.microsoft.com/en-us/dotnet/core/tutorials/cli-create-console-app
Use dotnet new console to create a new project in C#
This will create a .vscode folder in the directory
Create a tasks.json file
Create a Launch.json file and add configurations

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

A project with an Output Type of Class Library cannot be started directly

Please can someone could explain why I get this error and what to do to fix it (or what I'm doing wrong!). The steps I have taken are
Download Source code from http://www.codeproject.com/Articles/16859/AForge-NET-open-source-framework
Opening in VS2010 shows the references cannot be found
Re-Add all 3 references from PlayingCardRecognition\bin\Release so no further warnings
When I try and build or Run I get the following message
To fix this issue, do these steps:
Right click the Project name in Solution Explorer of Visual Studio
Select Set as StartUp Project from the menu
Re-run your project
It should work!
If it did not work, be sure that you have set your start page. If your project is C# Windows Application or C# Console Application, try this:
Right click the Project name in Solution Explorer of Visual Studio
Select Properties
Select the Application tab
In the Output Type drop box
Select the correct application type of your project
Re-run your project and let me know if it won’t work.
The project type set as the Start-up project in that solution is of type ClassLibrary. DUe to that, the output is a dll not an executable and so, you cannot start it.
If this is an error then you can do this:
A quick and dirty fix for this, if that is the only csproj in the solution is to open the .csproj file in a text editor and change the value of the node <ProjectGuid> to the Guid corresponding to a WinForms C# project. (That you may obtain from a google search or by creating a new project and opening the .csproj file generated by Visual Studio to find out what the GUID for that type is). (Enjoy - not many people know about this sneaky trick)
BUT: the project might be a class library rightfully and then you should reference it in another project and use it that way.
.Exe's and .dll's are both assemblies. The key difference is that executeables define an entry point Main which can be invoked by the runtime. The error
"Class library cannot be started directly"
is due to the fact that said .dll's do not have a Main. To fix this issue, change the project type to a Windows application/Console application and define an entry point. Or, add a new project that is of type Windows application/Console application and reference said .dll.
The project is a class library. It cannot be run or debugged without an executable project (F5 doesn't work!!!). You can only build the project (Ctrl+Shift+B).
If you want to debug the code add a console application project (set it as the start up project) to the solution and add the reference to the library.
The project you downloaded is a class library. Which can't be started.
Add a new project which can be started (console app, win forms, what ever you want) and add a reference to the class library project to be able to "play with it".
And set this new project as "Startup project"
The project you've downloaded is a class library, not an executable assembly. This means you need to import that library into your own project instead of trying to run it directly.
Your project type is a class library one would suspect, add a ConsoleApplication or WindowsApplication and use that as your startup object. Reference this project and then access the code.
If you convert the WPF application to Class library for get the projects .dll file.After that convert the same project to the WPF application you get the following error.
Error:".exe does not contain a static main method suitable for an entry point".
Steps to troubleshoot:
1.Include the App.xaml file in the respective project.
2.Right Click on App.xaml file change the build action to Application Definition
3.Now Build your project
Goto the Solution properties -> on Build right side you see the startup project type. here you need to select the console appication/windows appication.
If you got this issue (got it in Visual Studio 2017 RC), and you don't get any of the things listed by Mak post from step 3 onward "4 In the Output Type drop box....", it is because you made a Class Library app when you want to create a cross platform app, so here is the solution :
1 Start a new project
2 select Visual C# and cross-platform app.
3 select cross-platform app (Xamarin and native app)
4 select blank form.
From then , right click, select as startup project and build as mentioned by Mak, and it should work.
If you can afford to start from scratch, it could do the trick as it did for me.
This could do the trick for the main issue as well, but must be adapted to your current version of Visual Studio ("Xamarin.forms portable" for visual studio 2015 for example).
Bye!
VS -> Debug -> Attach unity debugger -> double click project
Set your api project to a startup project:
Right click the api Project than choose Set as startup Project.
Just right click on the Project Solution A window pops up. Expand the common Properties. Select Start Up Project
In there on right hand side Select radio button with Single Startup Project Select your Project in there and apply.
That's it. Now save and build your project. Run the project to see the output.
_Sarath#F1
To fix this issue, do these steps:
Right click the Project name in Solution Explorer of Visual Studio
Select Set as StartUp Project from the menu
Re-run your project It should work!

How to create a dll file

Using Visual Studio 2005
I have list of class files, when i try to run the class files, it showing error as
"a project with output type of class library cannot be started directly"
How to run the class file? How to create a dll file.
Am new to visual studio 2005
Need Help?
A Class Library is just that, a library of code, you need to create an application that references the library to try it out.
In the same solution, just add a new project as a Winforms Application and then in the winforms application project add a reference to the class library project.
You should then be able to call the methods in the library from the application code.
To create a DLL File, click on New project, then select Class Library.
Enter your code into the class file that was automatically created for you and then click Build Solution from the Debug menu.
Now, look in your directory: ../debug/release/YOURDLL.dll
There it is! :)
P.S.
DLL files cannot be run just like normal applciation (exe) files. You'll need to create a separate project (probably a win forms app) and then add your dll file to that project as a "Reference", you can do this by going to the Solution explorer, right clicking your project Name and selecting Add Reference then browsing to whereever you saved your dll file.
Then, to be able to use this dll file, in your projects code, you call the methods inside the dll file. For example:
If, in your DLL file you have a method like this:
public string somerandommethod()
{
string x = "something";
return x;
}
Then, in your Form1.cs file of your separate project, you would call the code from your dll file like this:
button1_Click(object sender, EventArgs e)
{
MyDllFile dll = new MyDllFile();
MessageBox.Show(dll.somerandommethod());
}
I hope this has helped you
You cannot run projects of type class library. You need to define a startup project that is either a console application, windows application or a web application which would use the class library.
If you are creating a library, look at using something like NUnit to test it. It will load the dll and execute whatever tests you define on it.
You can not run a class file, either you can go to project properties ->Application - > Output type. Here you can specify the application type as console application so your code will run on command prompt.
Also make sure that the project you are trying to run is set as startup project (you can do it by right click on project and select "Set as Startup project".
To create a DLL you need to select New Project -> Class library.

Categories

Resources