Visual Studio Code can't run C# file - c#

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

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.

Could not locate .NET Core project. Assets were not generated

I just started learning C# a couple hours ago, but have since run into this error.
Ctrl + Shift + P
.NET:Generate Assets for Build and Debug
But when I click it, an error pops up bottom right of the window:
Could not locate .NET Core project. Assets were not generated.
Restarting VS Code solved the problem in my case.
This same thing started happening to me on my mac on vscode. For me, it was because I didn't add my dotnet project to my solution file after I created the solution file and project.
This is essentially how I created my project:
Create empty project folder mkdir RecipeApp && cd RecipeApp
Add Solution File to Project (uses the name of containing folder) dotnet new sln
Create Backend .NET API dotnet new webapi -o API
Then this is the part I forgot to do (which actually solved a few other problems I was having, like I couldn't use the CMD + . feature to auto import classes and assets etc, too):
Add the sub project to the solution file dotnet sln add API
After I added the API subproject to the solution file, I could open the main top level project folder in vscode, and everything started working again.
This issue could be because the launch.json, which ideally should have been populated with the necessary details isn't generated in case. This issue seems to be linked to Omnisharp. If you go through the issue, you understand the root cause more clearly. You may need to perform the steps mentioned in this comment to get around the issue. These steps involve re-configuring the C# extension inside of your VS Code instance.
Alternately, ensure that all the folders which the extensions uses have appropriate access.
If the issue is also involving task.json, take a look at this answer
In my way helped to change the folder name to a name without spaces.
What I recommend doing is pressing "Run" then start debugging, which should open up a launch.json. If you open it, it will have about 17 warnings and then you can delete the warnings. Then, you press f5, which will give you an option to open a tasks.json. Do that and configure all of the tasks for running your project and it should work.
for me it helped to open the project with opening project_name.csproj with vs-code instead of loading the folder
In my case I just uninstall the c# extension and reload the vscode, after I install again (The vscode will ask to install some assets and somethings that miss in the project too).
Changing extension path manually helped me

My Visual Studio makes an .exe file instead of .dll

I was following this tutorial, but at step 5 I had to import a .dll file in PowerShell:
PS> Import-Module .\bin\Debug\PowerShellModuleInCSharp.dll
But my Visual Studio program only makes an .exe file. I tried importing the .exe file, but this gave an error
Import-Module : The extension '.exe' is not a valid module extension. The supported module extensions are '.dll', '.ps1', '.psm1', '.psd1', '.cdxml' and '.xaml'. Correct the extension then try adding the file 'C:\users\wouter\documents\visual studio`.
So is this because I am making a Windows application instead of an console application in Visual Studio?
This is how my Debug folder looks like:
No, you weren't following that tutorial. Quoting that tutorial (emphasis mine):
Step 1: Create a Visual Studio project
Within your Visual Studio solution, you will house your cmdlets in a project, just as you would any other component you are building. For PowerShell, create a Class Library project so that once you have built the project, you have a DLL that comprises your PowerShell cmdlets.
The project you created was not a class library project. You appear to have created a Windows Forms Application project instead.
While it's possible to fix this, it involves more than simply changing the project type to "Class Library". The Windows Forms Application project template contains a lot of things you neither need nor should want. It contains references to assemblies you don't want. It contains a form. It contains program start-up code. All of that will remain if you simply change the project type, and you would need to get rid of it manually. It's easier to start over, taking care to follow the steps of the tutorial exactly.
You need to change your project to be of type "Class Library" . For new projects its a matter of picking the "Class Library" template. For existing projects, there is a drop down list in the project properties screen that you can change to "Class Library" . Once you do that, you will need to recompile.
Go to Project > Properties and change the Output Type in the dropdown shown below in red to Class Library:

How do I set the "executable project as the startup project" in Visual Studio 2013 Express?

So I am trying to study a sample application called ModernUIDemo.exe found in a zip file here. The website mentions the source code of the app can be found here.
After downloading, and pointing Visual Studio Express 2013 for Windows Desktop to the solutions file and trying to start without debugging, I get an error message:
A project with an Output Type of Class Library cannot be started
directly.
In order to debug this project, add an executable project to this
solution which references the library project. Set the executable
project as the startup project.
I am assuming this means that there are multiple projects, and I need to set a startable project. So how do I do this?
I have been looking in the Application settings, where it lists "Startup objects" and it says FirstFloor.ModernUI.App.App. Isn't that correct?
Typically, how is documentation laid out in C# apps? Is there like a javadoc I have to generate for C#/XAML/WPF?
edit:
I am now getting some reference errors in the XAML to <mui:ModernTab and <mui:BBCodeBlock does not exist in XML namespace. Any clues? I already thought I added the dll file....
Right click on the project you want to run -> Set as start up project
In your project's property in Application tab you have to see:
Output type: Windows Application.
With this configuration after compilation you will get EXE file. Instead, according to the error message you get, your application is configured like Class Library, so output is DLL.

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!

Categories

Resources