I have the following C# code
class Test
{
public static void Main(String[] argv)
{
Console.WriteLine("Hello ");
}
}
When I run the program from the command prompt, I do not see output. The c# file name is file.cs and compiles into file.exe.
When I run this from the command prompt:
c:\>file.exe
I do not see any output. But it works if I run this:
c:\>file.exe | more
I understand I need to do something other that console.WriteLine(). Is there any way that I can redirect all Console.WriteLine() calls to standard output?
Application type was set to Windows application in Visual Studio project properties.
I set the application type to "Console". This fixed the issue. Now all Console.WriteLine statements gets printed to command window.
Related
I just bought my first Windows today (I have always used mac), but C# in my visual studio will not work. Running this:
using System;
public class Class1
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
just gives me this error and does not print anything out:
CopyWin32Resources failed with exit App3 code 500
I'm getting very frustrated can someone tell me how to get passes this?
Please choose the Console Application template in the New Project Dialog in the category Templates \ Visual C# \ Windows and copy your source code into the main method.
Then your program should start and shows a console with "Hello world".
If you add the line Console.ReadKey(); then the console window will remain open until you press a key.
With a Windows Console Application you can see output to the Console.
When attempting to run a C# console application without the debugger (ie Ctrl+F5) no output appears in the terminal.
When run with the debugger (ie just F5), program executes as expected.
I made a quick test project to make sure it wasn't just my project:
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test");
Console.ReadKey();
}
}
}
Run with the debugger, "Test" is output to console, without it, nothing.
I don't know what I've changed, because (my original) programme was running fine without the debugger before now
Problem was found to be Avast preventing the executable from running properly. Temporarily disabling the File System Shield acts as a workaround.
Visual Studio 14.0.23107.0 D14REL
Avast 10.4.2233 (virus definition version: 151130-0)
I am developing an application that writes some exception messages to the Console if they are of no use to the user. As a result if someone runs the executable by simply double clicking it, they will never see these messages.
In Visual Studio these messages show up in the output window, but in my case I am testing my application on a machine that does not a Visual Studio installation, though I still want to see if any of these messages appear.
In the past I have simply ran the executable from the command prompt and it acted as the output window in Visual Studio. Though for some reason my application, when ran from the command prompt, simple "returns" and does not show any messages.
For example I might start it like so, and it instantly returns
D:\>MyApp.exe
D:\>
I am not sure if there is a specific switch I should use (I have tried /K to no avail) when I run it, or if there is something about my application that causes it to return.
Any ideas on how I might run it via the command line so I can see the messages?
For reference here is my applications Program.cs
static class Program
{
static Mutex mutex = new Mutex(true, "{12345678-1234-1234-1234-123456789012}");
[STAThread]
static void Main()
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SplashScreen.ShowSplashScreen();
Application.Run(MainForm.Instance);
mutex.ReleaseMutex();
}
else
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_JTTMAINWINDOW, IntPtr.Zero, IntPtr.Zero);
}
}
To allow your program to output to the console, build it as a console application.
In Visual Studio, the relevant linker option is /SUBSYSTEM:Console ; or, when creating a new project, choose the "console application" template which will set the linker option automatically.
This will also mean that if you run the program from the command prompt, the command prompt will wait for the program to exit. Also, if you double-click the program rather than running it from the command prompt, a console window will automatically be created.
(There is no way to make the command prompt wait for a program but prevent the program from creating a console window if double-clicked. See this question and its answers for more information.)
I seem to have come across what may be a bug in mono, or that Im doing something wrong.
I use subprocesses to do things via C# however in some situations the terminal breaks by not showing what Im typing correctly. Ive managed to get this down to happening after using the Console to read a line.
Im currently on a Mac using mono version Mono JIT compiler version 3.10.0, but dont know if this is a mono thing or it can happen on Windows.
The minimum code I have been able to recreate the bug in is bellow:
using System;
using System.Diagnostics;
namespace Test
{
class MainClass
{
public static void Main( string[] args )
{
Console.ReadLine();
Process.Start("tee","text.txt").WaitForExit();
}
}
}
If you remove the read line, the output from tee will be printed after you input it (so you will see the same line twice) but with the read line it only displays the output from tee, not the input you type
IE:
Without the read line
~/test $ mcs program.cs && mono program.exe
this
this
is
is
writting
writting
to
to
file
file
^C
~/test $ cat text.txt
this
is
writting
to
file
And with the read line
~/test $ mcs program.cs && mono program.exe
readline
this
should
be
printing
as well
~/test $ cat text.txt
this
should
be
printing
as well
I wrote a simple Hello World program in C# using Visual Studio 2013. I tried to compile it on the command line in Linux using:
mono --aot test.cs
However when I do that, I get the error:
Cannot open assembly 'test.cs': File does not contain a valid CIL image.
The file is just a typical C# console application using the default template that Visual Studio gives you.
You should use gmcs in order to compile your code, and mono to execute the interpreter, as one use javac and java commands.
You may reread the mono basics:
Let's say you have a C# file with the following code:
using System;
public class HelloWorld
{
static public void Main ()
{
Console.WriteLine ("Hello Mono World");
}
}
Compiling within the shell:
gmcs hello.cs
Executing it from the shell:
mono hello.exe