Setting console title doesn't work

V

vijai.kalyan

Hello,

I wrote a trivial program to set the console title in a console
application. Here is the code:-

namespace myns
{
class SetTitle
{
static void Main ( string [] args )
{
if ( 0 >= args.Length || 0 >= args [ 0 ].Length )
System.Console.WriteLine ( System.Console.Title );
string title = args [ 0 ];
if ( title.StartsWith ( @""" ) )
title = title.Substring ( 1 );
if ( title.EndsWith ( @""" ) )
title = title.Substring ( 0, title.Length - 1 );
System.Console.Title = title;
}
}
}

However, when executing from the command line (ran cmd.exe and switched
to output dir), the shell title does not change.

On the other hand, if I step through the program in Visual Studio, the
console popped up by Visual Studio does have its title changed.

Any ideas on this?

thanks,

-vijai.
 
V

vijai.kalyan

I tried simplifying the program as follows, but that didn't work
either.
namespace myns
{
class SetTitle
{
static void Main ( string [] args )
{
if ( 0 >= args.Length || 0 >= args [ 0 ].Length )
System.Console.WriteLine ( System.Console.Title );
System.Console.Title = title;
}
}
}

thanks,

-vijai.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I wrote a trivial program to set the console title in a console
application. Here is the code:-

namespace myns
{
class SetTitle
{
static void Main ( string [] args )
{
if ( 0 >= args.Length || 0 >= args [ 0 ].Length )
System.Console.WriteLine ( System.Console.Title );
string title = args [ 0 ];
if ( title.StartsWith ( @""" ) )
title = title.Substring ( 1 );
if ( title.EndsWith ( @""" ) )
title = title.Substring ( 0, title.Length - 1 );
System.Console.Title = title;
}
}
}

However, when executing from the command line (ran cmd.exe and switched
to output dir), the shell title does not change.

Are you aware that the title is restored when the program
exits ?

And when run in non debug the time between setting the title
and exiting is very short.

Arne
 
V

vijai.kalyan

But, I find that very weird. Setting the console background or
foreground colors persists even after the application that set them
exits. Then why not the title?

-vijai.
I wrote a trivial program to set the console title in a console
application. Here is the code:-

namespace myns
{
class SetTitle
{
static void Main ( string [] args )
{
if ( 0 >= args.Length || 0 >= args [ 0 ].Length )
System.Console.WriteLine ( System.Console.Title );
string title = args [ 0 ];
if ( title.StartsWith ( @""" ) )
title = title.Substring ( 1 );
if ( title.EndsWith ( @""" ) )
title = title.Substring ( 0, title.Length - 1 );
System.Console.Title = title;
}
}
}

However, when executing from the command line (ran cmd.exe and switched
to output dir), the shell title does not change.

Are you aware that the title is restored when the program
exits ?

And when run in non debug the time between setting the title
and exiting is very short.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

But, I find that very weird. Setting the console background or
foreground colors persists even after the application that set them
exits. Then why not the title?

Good question.

I do not have the answer.

Arne
 
W

Willy Denoyette [MVP]

That's normal, the console colors are persisted (in the registry), the title
is volatile , it remains for the cmd session and is initially set by
cmd.exe.
Note that changing the console colors in an application without restoring
the original colors back is a bad coding practice, an application running in
the command interpreter (cmd) is not owning the console window, it's the
cmd.exe who owns the console, so you need to restore all it's properties
when done with your application.

Willy.

But, I find that very weird. Setting the console background or
foreground colors persists even after the application that set them
exits. Then why not the title?

-vijai.
I wrote a trivial program to set the console title in a console
application. Here is the code:-

namespace myns
{
class SetTitle
{
static void Main ( string [] args )
{
if ( 0 >= args.Length || 0 >= args [ 0 ].Length )
System.Console.WriteLine ( System.Console.Title );
string title = args [ 0 ];
if ( title.StartsWith ( @""" ) )
title = title.Substring ( 1 );
if ( title.EndsWith ( @""" ) )
title = title.Substring ( 0, title.Length - 1 );
System.Console.Title = title;
}
}
}

However, when executing from the command line (ran cmd.exe and switched
to output dir), the shell title does not change.

Are you aware that the title is restored when the program
exits ?

And when run in non debug the time between setting the title
and exiting is very short.

Arne
 
K

kalyanapasupathy.v

Ah. True. But, I am trying to write a set of utilities to customize the
command window. I use 4NT at work and would like to be able to
customize the command shell like 4NT allows. Just an interesting though
possibly useless project :)
That's normal, the console colors are persisted (in the registry), the title
is volatile , it remains for the cmd session and is initially set by
cmd.exe.
Note that changing the console colors in an application without restoring
the original colors back is a bad coding practice, an application runningin
the command interpreter (cmd) is not owning the console window, it's the
cmd.exe who owns the console, so you need to restore all it's properties
when done with your application.

Willy.

But, I find that very weird. Setting the console background or
foreground colors persists even after the application that set them
exits. Then why not the title?

-vijai.
I wrote a trivial program to set the console title in a console
application. Here is the code:-

namespace myns
{
class SetTitle
{
static void Main ( string [] args )
{
if ( 0 >= args.Length || 0 >= args [ 0 ].Length )
System.Console.WriteLine ( System.Console.Title );
string title = args [ 0 ];
if ( title.StartsWith ( @""" ) )
title = title.Substring ( 1 );
if ( title.EndsWith ( @""" ) )
title = title.Substring ( 0, title.Length - 1 );
System.Console.Title = title;
}
}
}

However, when executing from the command line (ran cmd.exe and switched
to output dir), the shell title does not change.

Are you aware that the title is restored when the program
exits ?

And when run in non debug the time between setting the title
and exiting is very short.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top