Where can i run my code in visual studio? - c#

So, i am trying to write my first C# "Hello World" Program in visual studio, however when i want to see the output of the code (or simply just run it), i can't because i can find the run option, i have tried ctrl + f5, and many other options, however it didn't worked any suggestions? Here's a picture of my visual studio.

Check this guide Tutorial: Create a simple C# console app in Visual Studio
Open Visual Studio 2019.
On the start window, choose Create a new project.
On the Create a new project window, enter or type console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.
In the Configure your new project window, type or enter Calculator in the Project name box. Then, choose Create.
And then to run:
Choose the green Start button next to Calculator to build and run your program, or press F5.

This is because you created empty solution. You don't have any output, i.e. process of compilation of your solution does not produce any executable (exe) or library (dll).
In order to run any code is to produce one of the above. In order to do that you have to have project created.
To do so, you have to (in your case) right click solution and choose "Add -> New Project" option. This will show you pop up allowing you to choose which project you want to add, choose just basic type of project, which is "Console app". There you will have class with static method Main which is the entry point for your app.
Code inside Main method will be executed.

You haven't created a project. You've got no executable and no ability to build one. I don't know if you've worked with other languages before or not (perhaps ones which are interpreted and/or have no specific structure to them), but C#/.NET programs require a particular structure in order to build and execute. You can't just create a random C# file on its own and execute it.
Instead, create a new Console Application project (from the File -> New Project menu) and give it a name.
That will auto-generate a Program.cs file, which contains the Main method which is the entry point of your program when it's executed. From there you can write code in there, create other methods in that class if you need to, and you can also add other files containing classes etc. into your project.
(There are, as you'll see, also quite a lot of other project types you can create depending on the kind of application you want - and also some types which are just for code libraries and not full applications.)

How to: Run a C# program in Visual Studio
The project should contain a C# file with a Main method, and its output should be an executable (EXE), you have created an empty solution with no such files.

Related

How to debug run a project as a different user in Visual Studio?

I need to debug run a C# project using a different user. I found this link, However, it's old.
How do you debug a project as a different user in Visual Studio 2022?
The Launch Profiles doesn't have the executable field.
If you mean how to using the approach for a .net core project, here is the steps:
Click the downward arrow in the picture and choose "<project> Debug Properties".
In the dialog click the first button and choose "Executable".
Input the executable and arguments.
Select the new profile (in the first picture) and start debugging.
Note that with this approach you still need attach to the process, see the comments in this answer: https://stackoverflow.com/a/1287221/6196568

How to run separate files in Visual Studio 2019 C#

I am trying to run another file in Visual Studio 2019. I have the Program.cs and then Variables.cs which is dedicated to variables. I am following a tutorial.[Here i have 2 files Program.cs and Variables.cs. How can i run Variables.cs. If i try and run it i get an error called "There is already a main entry point". Also for the near future lets say i created a file dedicated o arrays or user input. How can i run user input or arrays? Also how can i also have 2 or more C# files in VSCode without getting a problem when running. I am also a beginner in C#
When you right click a different project in your solution, regardless of whether another main() function is already running, you can see a debug option. Mousing over this will allow you to hit Start New Instance, which will run that project simultaneously.

opening c# files with visual studio outside of project gives no suggestions

Im currently writing some very small c# exercises, for a algorithm course.
visual studio is my favourite IDE, and usually, when i create or clone a visual studio project, I get the full functionality of viusal studio including spellchecking and suggestions for fields and methods on objects and so on.
But right now I am just trying to open a single .csc file and write some code in it. The problem is that when i do that, i get no suggestions. So if I create a list I would usually be able to view all the methods and fields inside the list class simply be referencing an object. Syntaxm checking works fine.
How do I turn on intellisense suggestions in a file that is not in a project?
Thank you
The simplest way is simply to add that file to a project.
Open VisualStudio.
Create a new project (you can probably use Console project or Class library, depending on what you're doing).
Add your file in that project.
Make sure your file as the Build Action C# Compiler.
And that should work.

Generate Winforms Project .exe programmatically from within a Winforms Application

I have a requirement to build a container C# WinForm Application which will spit out .exe files of another WinForm Application on a button click event.
For eg: I have one Winform App named ProjectA which accepts one startup argument. Now I have a container WinForm App named ProjectB. I want to generate ProjectA.exe programmatically within ProjectB by passing the required parameter to ProjectA app on the button click.
Could not find anything relevant about this on Google. Can anyone throw in some light to achieve this.
Please note that both the Winforms Application must be written in C#.
I have one very vague thought of using MSBuild Command to build ProjectA which in turn will generate its .exe
However I am not too clear on this.
It is not necessary to mess with MSBuild for this. You may generate your program's code as a string and then generate your executables on the fly:
https://support.microsoft.com/en-us/help/304655/how-to-programmatically-compile-code-using-c-compiler
As an example:
Process.Start(#"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe", #"d:\app1\app1.sln");
Another Example:
Process.Start(#"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe", #"d:\app1\app1.sln /t:Rebuild /p:Configuration=Debug;TargetFrameworkVersion=v4.0");
put the above code on your button click.

How can I run multiple programs sequentially, from one solution?

I have a Visual studio solution containing multiple programs, each of which contains it's own Class and Main Method. I need to set these programs up to run one after the other, as some of them access the same database tables, and I do not want there to be conflicts.
I have tried setting up dependencies and creating a new program with class dependencies, however, this does not seem to have worked, does anyone have an idea?
Right click on your solution > Properties > Common Properties > Startup project
Select "Multiple startup Projects" and change the value of the Action column from "None" to "Start" or "Start without debugging"
Visual Studio is a development tool. It is not a tool to orchestrate how your application should execute. Your application should also function when executed outside Visual Studio.
With that in mind you need to decide how to partition your application. Right now you seem to have several applications (console programs?) that need to execute one after the other. An easy solution is to create a batch file that executes each in turn. You can also create a separate application that execute each "child" application in sequence using the Process.Start method but why bother when you can create a batch file?
You might also consider merging your different applications into a single application that has a Main method that executes each task in sequence.
Without more information about your problem it is hard to give you specific advice.
You mentioned you have tried "Multiple Startup Projects" approach but did you set the order in which multiple projects run when you start the debugger?
To set the order in which multiple projects run when you start the debugger
Open the Solution Property Pages dialog box.
Select the Startup Project set under Common Properties.
In the pane on the right, select Multiple Startup Project.
Select a project, and then click Move Up to run that project earlier when you start the debugger, or click Move Down to run the project later.
Reference: How to: Modify Project Properties and Configuration Settings
You need run each program in separate Visual Studio instances.

Categories