Console.CursorVisible Property

Definition

Gets or sets a value indicating whether the cursor is visible.

C#
public static bool CursorVisible { [System.Runtime.Versioning.SupportedOSPlatform("windows")] get; [System.Runtime.Versioning.UnsupportedOSPlatform("android")] [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] [System.Runtime.Versioning.UnsupportedOSPlatform("ios")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] set; }
C#
public static bool CursorVisible { [System.Runtime.Versioning.SupportedOSPlatform("windows")] get; [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] set; }
C#
public static bool CursorVisible { get; set; }

Property Value

true if the cursor is visible; otherwise, false.

Attributes

Exceptions

The user does not have permission to perform this action.

An I/O error occurred.

The get operation is invoked on an operating system other than Windows.

Examples

This example demonstrates the CursorVisible property. The example makes the cursor visible if the first column of input is a '+' character or invisible if the input is a '-' character.

C#
// This example demonstrates the Console.CursorVisible property.

using System;

class Sample
{
    public static void Main()
    {
    string m1 = "\nThe cursor is {0}.\nType any text then press Enter. " +
                "Type '+' in the first column to show \n" +
                "the cursor, '-' to hide the cursor, " +
                "or lowercase 'x' to quit:";
    string s;
    bool saveCursorVisibile;
    int  saveCursorSize;
//
    Console.CursorVisible = true; // Initialize the cursor to visible.
    saveCursorVisibile = Console.CursorVisible;
    saveCursorSize  = Console.CursorSize;
    Console.CursorSize = 100;     // Emphasize the cursor.

    while(true)
        {
        Console.WriteLine(m1,
                         ((Console.CursorVisible == true) ?
                           "VISIBLE" : "HIDDEN"));
        s = Console.ReadLine();
        if (!String.IsNullOrEmpty(s))
            if (s[0] == '+')
                Console.CursorVisible = true;
            else if (s[0] == '-')
                Console.CursorVisible = false;
            else if (s[0] == 'x')
                break;
        }
    Console.CursorVisible = saveCursorVisibile;
    Console.CursorSize    = saveCursorSize;
    }
}
/*
This example produces the following results. Note that these results
cannot depict cursor visibility. You must run the example to see the
cursor behavior:

The cursor is VISIBLE.
Type any text then press Enter. Type '+' in the first column to show
the cursor, '-' to hide the cursor, or lowercase 'x' to quit:
The quick brown fox

The cursor is VISIBLE.
Type any text then press Enter. Type '+' in the first column to show
the cursor, '-' to hide the cursor, or lowercase 'x' to quit:
-

The cursor is HIDDEN.
Type any text then press Enter. Type '+' in the first column to show
the cursor, '-' to hide the cursor, or lowercase 'x' to quit:
jumps over

The cursor is HIDDEN.
Type any text then press Enter. Type '+' in the first column to show
the cursor, '-' to hide the cursor, or lowercase 'x' to quit:
+

The cursor is VISIBLE.
Type any text then press Enter. Type '+' in the first column to show
the cursor, '-' to hide the cursor, or lowercase 'x' to quit:
the lazy dog.

The cursor is VISIBLE.
Type any text then press Enter. Type '+' in the first column to show
the cursor, '-' to hide the cursor, or lowercase 'x' to quit:
x

*/

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1