Can't change Console.Title on VS2019 C# - c#

Just starting with C# and I'm trying to do very simple things, one of these is changing the console title.
I'm following these instructions: Console.Title Property
The link above is from the Microsoft documentation, and when I copy it in my program it works!
When I try to do the same thing, even simpler... well the title doesn't change at all.
My code:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Hello World Program";
}
}
}
My output:
What am I doing wrong? There is some extra step that I must take which I'm not aware of?
Thank you for your time and help.

After your program exits, Console will no longer have the title you set it to. Use a breakpoint at the end to see what the title is before your program exits.
Console.Title = "New";
return; // Set a breakpoint here.
or you can simply add a 'press any key to continue' (as per the MS docs)
Console.WriteLine("Note that the new console title is \"{0}\"\n" +
" (Press any key to quit.)", Console.Title);
Console.ReadKey(true);

Related

When using input = Console.ReadLine(); it doesnt go to the next line of code, it simply loops when I put anything

Quick note: I come from python and other easier to use programming languages.
I dont know why it doesnt go and continue to the next line, this is the steps I took, I firstly run the script in visual studio and it runs and then I can input text into the debug console, I type something and it repeats the line of input so I input again and it doesnt go onto the next line it just loops at input = Console.ReadLine();, I even tried copying code from the internet that simply asks for input and prints it and it does the same thing, I think its something to do with visual studio maybe, This is a first so I apologize if I made a mistake, thank you for your help.
here is the code:
using System;
using System.IO;
namespace Printing
{
class Program
{
static void Main(string[] args)
{
string input;
Console.WriteLine("Please enter a number for the times table");
input = Console.ReadLine();
Console.WriteLine("number is: " + input);
for (int i = 0; i < 10; i++)
{
if (i != 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
}
}
OMG This was very annoying and finally found a way of fixing this, for what ever reason the internal console does not work at all at least on my linux system, I found a post 4 years old mention changing the console to integratedTerminal and that fixed it, it works now, to change the console go to your launch.json file and change internalconsole too integratedTerminal.
very frustrating but now its done.
Something may be wrong with your Visual Studio. I've run your code in my computer and it works perfectly fine, nothing loops. Is VS updated to the latest version? Check Visual Studio Installer. And what console are you using? Is it the default Windows CMD? My advice would be to reinstall VS if it still doesn't work.

Passing args from a batch file to a C# program with a space

I'm trying to pass arguments/parameters from a batch file to a simple C# Console.Writeline(args[0]) program.
My program's exe file has a two-word name with a space and that's the problem! The whole thing only works when I rename the exe file to a whole word and write start SchoolBook.exe OMG and the arg[0] then becomes equal to "OMG".
But in any other case the program won't start.
If I leave it start School Book.exe OMG it says "Windows cannot find "School".. "
If I put double quotes: start "School Book.exe" OMG it says "Windows cannot find "OMG".. "
If I put single quotes it can't find "School"
I tried putting a '-' before the OMG, putting quotes around the OMG, even putting %1% before it NO! Nothing works..
Any help?
Thank you in advance!
I just created an empty command application "School Book" which when built produces the executable School Book.exe.
My pretty empty implementation:
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
MessageBox.Show(args[0]);
}
else
{
MessageBox.Show(args[0]);
}
}
}
In my command box I can execute the program like this:
Debug>start "" "School Book.exe" OMG
Now a messagebox pops up with "OMG"

VS 2010 - output window - how to enable "find message in code" button?

When I build a Windows Forms project in VS2010 in debug mode (F5) a window appears titled Output Window. There are buttons along the top of the Output Window: "Find Message in Code", "Next Message", and "Previous Message". However, they are always grayed-out (disabled). How do these become enabled?
I'm expecting the intent of these buttons is to help me find lines of code that caused a message to appear in the Output Window. For example, if I write Trace.WriteLine("MyMessage"); in client code, when executed it will make 'MyMessage' appear in the Output Window; I thought by selecting a message in the Output Window and clicking "Find message in Code" it would navigate to the line of the client code that contains "MyMessage". This would be a slick feature if it were enabled and my presumptions were correct. Unfortunately I can't get the buttons to enable.
To answer this question please explain how to enable and use these buttons and if any best practices should be applied (optional).
Here is some source code to use as reference. Create a Windows Forms project and make the changes you see below and you can reproduce what I am attempting.
// program.cs
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace MyNamespace
{
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
throw new ApplicationException("MyMessage");
Application.Run(new FormMain());
}
catch (ApplicationException e)
{
Trace.WriteLine(e.ToString());
}
}
}
}
JasonD's recommendation
I replaced
Trace.WriteLine(e.ToString());
with
Trace.WriteLine("Program.cs" + "(" + 23 + ")" + " Some info");
End JasonD solution - result: buttons enabled
That solved it. And what an unexpected answer. In this era of strongly typed everything the answer depends on formatting strings with magic messages. I'm surprised by this.
You need to output something it can parse, which is essentially the same format as error/warning messages you see when compiling, consisting of "FILENAME(LINE) stuff".
In C#, something like this:
string file_= new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
int line_= new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber();
System.Diagnostics.Trace.WriteLine(file_+ "(" + line_.ToString() + ") Some info");
(this is a little messy, but there's no good equivalent of C/C++'s __FILE__ and __LINE__ macros that I've found)
You can tidy that up a bit and wrap it in a function, but you need to get the file/line of the caller, not the actual function itself:
static void traceFunc(string msg)
{
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);
string file = trace.GetFrame(1).GetFileName();
int line = trace.GetFrame(1).GetFileLineNumber();
System.Diagnostics.Trace.WriteLine(file + "(" + line.ToString() + ") " + msg);
}

How to change application settings (Settings) while app is open?

I've written a class that should allow me to easily read and write values in app settings:
public static class SettingsManager
{
public static string ComplexValidationsString
{
get { return (string)Properties.Settings.Default["ComplexValidations"]; }
set
{
Properties.Settings.Default["ComplexValidations"] = value;
Properties.Settings.Default.Save();
}
}
the problem is the value isn't really saved, I mean it is not changed when I exit the application and run it again. What can I do to ensure that the saved value persists between closing and opening again?
settings scope must be user not application
You should check
Properties.Settings.Default.Properties["ComplexValidations"].IsReadOnly
It is probably true, this is what Roland means with "Application Scope". Save will fail silently. Take a look at Project|Properties|Settings, 3rd column.
Are you sure it's not saving the changes? The [ProgramName].exe.config file in the bin folder won't be updated. The acutal file used is usually put in C:\Documents and Settings\[user]\Local Settings\Application Data\[company name]\[application].exe[hash string]\[version]\user.config. I know when I tried this kind of thing it took me a while to realise this was the file that was getting updated.
I just tested a User Setting and it is persisted if you run this Console app twice:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Settings1.Default.Setting);
Console.ReadLine();
Settings1.Default.Setting = "A value different from app.config's";
Settings1.Default.Save();
}
}
Just try it out. It won't take a minute.

Reading console input in MonoDevelop

I am trying to a simple C# program that takes input and passes it as output. For instance, the output should be:
What is your name?
{user input}
Your name is {user input}
The program is:
public static void Main(string[] args)
{
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("Your name is: " + name);
Console.ReadKey();
}
This is enclosed in a class called 'MainClass'
Its output is:
What is your name?
Your name is:
Why is this not working and how can I make it work?
P.S. I am using MonoDevelop and I added Console.ReadKey(); after the last WriteLine. No change.
You are trying to type in the Application Output window in MonoDevelop and it is read-only.
You can configure MonoDevelop to automatically run the program at the command prompt by right clicking on the "options" menu item of your project and checking Run on external console under the Run > General tree.
alt text http://psf.biz/public/monodevelop_run_on_external_console.jpg
I guess the guy that gave me the -1 was blinded by that huge "Works on My Machine" emblem, nevertheless this is the correct and only answer.
(source: typepad.com)
Is your problem that the program quits immediately after reading the console input? If so, then add a Console.ReadKey(); after the last WriteLine so the program will wait for a keypress. Otherwise, I don't know what the problem is; I copy+pasted the code and it worked.

Categories