Compiling C# Code in Visual C# 2010 Express - c#

I just have one question regarding C#.
I have downloaded Microsoft Visual C# 2010 Express to write the C# code.
Now, I want to compile the code using the same visual C#. Can I?
I have searched for a method to compile the code but all of the methods I founded are talking about command line 'cmd'.
I tried using it but it gives me that "csc is not recognized as an internal or external command ....." although that the directory I was in is the same as the C# code directory
I want to compile and see the results in the output.
This is the original code to compile:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project1
{
class Example1_1
{
public static void Main()
{
// display “Hello World!” on the screen
System.Console.WriteLine("Hello World!");
// display the current date and time
System.Console.WriteLine("The current date and time is " +
System.DateTime.Now);
}
}
}
and the result of pressing F6 'build' is:
------ Build started: Project: Project1, Configuration: Release x86 ------
Project1 -> C:\Users\Ali Alnufaili\AppData\Local\Temporary Projects\Project1\bin\Release\Project1.exe
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
If you have any suggestion, till me please. ^_^

Just press this button:
or hit F5 to compile and run your code.
EDIT:
So you are running a console application and write some text to the console. Maybe your problem is that the console window pops up and closes immediately?
Try adding System.Console.ReadKey(); at the bottom of your Main method. Then the console window will stay open until you hit a key.
Or go the directory where your compiled program is (looks like it is C:\Users\Ali Alnufaili\AppData\Local\Temporary Projects\Project1\bin\Release), open a command prompt (in windows explorer, hold down SHIFT and press the right mouse button and choose Open command prompt here), and run the executable in the command prompt (just type Project1.exe and hit enter)

If you've created a project in Visual Studio, you should be able to simply use the "build" option from the menus/toolbars.
If you've got the default C# key bindings, I believe F6 is a shortcut to start under the debugger.

I'm sure you have used some tutorial for the example. Did the tutorial not mention pressing F5 to compile and run the application?
Your Build output says your program has compiled successfully, but to start your application, too, you need to press F5.
Also, currently your program will immediately exit when done. To make it wait, add the following line at the end:
Console.ReadLine();
That will let you see your output and then the program will wait until you press enter.

Related

Console immediately closes with CTRL+F5

In Visual Studio 2017, I'm using C# to create a basic hello world application.
CTRL+F5 (Start without debugging) immediately shuts down without showing the output.
I've followed this solution the second most upvoted answer
EDIT: Apparently this solution doesn't apply to C#
I've also tried resetting my settings and uninstalling visual studio. I don't want to add breakpoints or ReadLine.
Bottom line, I just want my program to not exit on CTRL+F5.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world");
}
}
}
Did you create this using the console app template? If not, try doing that.
You can check that it's set correctly by making sure that the output type (properties --> Application --> Output type) is set to 'Console Application' for this to work when pressing CTRL + F5
in a console app you need some sort of wait after you print to the screen or the app will exit. try adding Console.ReadLine(); it will then wait until you press enter to exit.
EDIT:
if you don't want to do that then run it from a command prompt to avoid the auto close behavior that Visual Studio adds.
Jay pointed out in a comment that maybe I used the wrong template. I don't know what template I used before, but I remade the application using the Console template and it works now.
Make sure that command line arguments have one line only. Check your start-up project Properties | Debug | Start options | Command line arguments.

How do I display clean output in Visual studio code with out path or "You may only use..." disclaimer?

I have this simple code in Visual Studio Code
class Program
{
static void Main()
{
Console.WriteLine("Hello!!!");
}
}
All I want to do is get the same result as when I type DOTNET RUN which prints a simple "Hello!!!" to the terminal.
Instead, if i configure it to use the terminal, I get this PATH in blue or if I try to use the debug window, I get this "You may only use..." disclaimer.
How can I set my configuration to either use one of these windows by clicking "Start with out debugging" (not via typing dotnet run manually) to print my output with out any excess info in the red boxes as seen in the images?
If there's a way to configure "dotnet run" in my settings, i'm good with that too so long the output is just "Helloo!!!" on either the debug or terminal window.
You might be able to do this by using an extension like Code Runner which works with Visual Studio Code, it is similar to Script Runner which works with the Atom editor. These are useful for reviewing simple code changes in real-time.
For Python i tried the method below.
In Visual Studio Code, click on the file and then click “RUN” at the top. find “Add Configuration” button on the menu and create a launch.json file by clicking on it. In the file, you will the line ”console”: integratedTerminal . Now change it to “console”:internalConsole. Open your code file and click “RUN” at the top again. Finally if you click on “Run without debugging” button, the output of your code will be display in “Debug Console” at where the terminal is.
Hope it works for you too.

C# Console Application build but not launch application

I have a simple Console Application write with C-Sharp language (Visual Studio 2013):
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When I press F5 or click Start button, my project was built, but not launch.
Sometime, Ouput windows says:
Error 12 Could not copy "obj\Debug\HelloWorld.exe" to "bin\Debug\HelloWorld.exe". Exceeded retry count of 10. Failed.
Error 13 Unable to copy file "obj\Debug\HelloWorld.exe" to "bin\Debug\HelloWorld.exe". The process cannot access the file 'bin\Debug\HelloWorld.exe' because it is being used by another process.
but when I write Windows Form Application, my project was built and launch normally ???
Why? and How to solve this problem ?
This is most likely due to windows keeping the process open. Your only option is to try and kill all processes of your app in task manager->processes.
The next thing to try is to simply change the build from debug to release, this should build another executable in the release folder as opposed to debug. By no means is this a silver bullet but, hopefully a sufficient workaround.
Before you try the release build, attempt to try and fix the problem. I've seen windows moan at just having my debug folder open and my exe selected because I suspect the thumbnail was being displayed thus being "used" in windows etc.
I found reason and solve for my problem.
I tried to restart Application Experience Service and the problem was solved.
Please restart yor Visual Studio and then try again. It will work and delete your bin/debug folder again.
I have faced with this issue.
That happeneds when an instance of software is running whether by visual studio or by your self
solution :
you should find your application name in the processes of task manager
your's is :
HelloWorld.exe
select that and click on the end task button
now you can start debugging and running you'r app.
at least worked for me.

Visual Studio clears output file at end of debugging

I am experiencing a weird behaviour from Visual Studio 2013. I've got a C# program which writes to the standard output, let's say
using System;
using System.Threading;
namespace CsSandbox
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
Thread.Sleep(10000);
}
}
}
In the Debug tab of the project's properties I have redirected the output to a file, like this:
If I open the file within those 10s when my application is still running, the file does contain "Hello world!". However, as soon as the program exits, the file is cleared. This does not happen when I run the program from the command line.
Is there a rationale why Visual Studio does it? Is there a way to bypass this behaviour?
I believe this is due to the way Visual Studio hosts your application, in order to reduce startup time when debugging.
What happens is that your application (Program.exe) will actually be hosted in another process (Program.vshost.exe), which is started with the same command line arguments. When your application ends, this process is immediately restarted. You should be able to see this within Task Manager - look in the details tab so you can see the PID of the processes, and run your app - you'll see one Program.vshost.exe instance which ends when your app finishes, and another one come up immediately. That's then overwriting your output file.
One fix for this is to give your application a command line argument for the file to write to - and open that file within your Main method. You won't get there until you start running the app again.
Another option would be to simply stop using the hosting process - in the Debug part of your project properties, at the bottom you should see a checkbox for "Enable the Visual Studio hosting process". Uncheck this and I think your problem will go away - but it'll take longer to start debugging.
See this blog post for more information about the hosting process.

How does VS compile console applications to show "Press any key to continue"?

When I develop a C# console application (which will run on a server) and I run it using Visual Studio, I get a "Press any key to continue" message before the program terminates.
However, when I compile the very same C# code file manually using CSC, my program doesn't show that message and it terminates immediately after finishing its logic.
Does anyone know how I can make the same feature when compiling the code without using VS and WITHOUT changing the C# code any adding a ReadLine()?
UPDATE : The same message used to appear when I learned C#, I used to use TextPad with CSC, and that message used to appear without adding any Write(Line)/Read(Line) callings
It's nothing to do with the compiler - if you press F5 to debug it rather than Ctrl-F5 to run without debugging, then VS doesn't show the prompt. This is presumably so that you don't miss whatever output it's producing.
In order to do this, Visual Studio runs cmd.exe telling it to run your executable and then pause:
"C:\WINDOWS\system32\cmd.exe" /c ""...\ConsoleApplication1.exe" & pause"
It probably doesn't do it when you debug as it's a bit harder to get the process ID of a child of a child process.
To add a similar option to your program, either use a command line switch to tell the application itself to pause, or use a batch file to run it then pause, or use a shortcut with them cmd.exe /c.
That's not possible. The prompt to press any key is generated by Visual Studio when running a console app. It's not part of your program.
The only way is by using Console.Read() in your code
UPDATE: concerning your remark on using TextPad: I'm not familiar with TextPad, but I wouldn't be surprised if TextPad did the same thing as Visual Studio when running a console app.
You could do this...
static void Main(string[] args)
{
#if DEBUG
Console.Read();
#endif
}
That way the program will not wait for the console input when you build your application as a 'Release'.
You could write a batch script that runs the exe for you and prompts the user to press a key.
The batch script would look something like this:
echo off
YourApp.exe
pause
You could do this, if you want the same functionality when debugging.
if (Debugger.IsAttached)
{
Console.WriteLine("Press any key to continue . . . ");
Console.ReadKey(true);
}
This behavior has nothing to do with the compiler you are using. When you compile with Visual Studio, running the executable outside of Visual Studio actually will perform exactly the same as when you compile with CSC on the command line. Visual Studio (and TextPad) is adding the logic to add the "Press any key to continue" message on the console.
If you want your application to stay open, you will need to do something like Console.ReadLine() to block execution so that your application does not complete its execution.
The question is why would you want to have this behaviour? The Press any key to continue feature is there so that you can see the output of your application. If on the other hand you build your code and run it from a command prompt (console), this will not close when the application finishes, so you can see the output.
As noted above, the Press any key to continue is a feature of the IDE and not related to the code you are writing. The purpose of that feature is to allow you to see the output of you console application.

Categories

Resources