Csharp/C Sharp by API/System/Console

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Console.BackgroundColor

 
using System;
public class Test{
   static void Main(string[] args){
      Console.Title = "Standard Console";
      Console.ForegroundColor = ConsoleColor.Red;
      Console.BackgroundColor = ConsoleColor.Green;
      Console.WriteLine("Press Enter to change the Console"s appearance.");
      Console.ReadLine();
  }
}


Console.Beep()

 

using System;
using System.Windows.Forms;
using System.Media;
public class MainClass {
   public static void Main() {
       Console.Beep();
   }
}


Console.Beep(

 
using System;
using System.Windows.Forms;
using System.Media;
public class MainClass {
   public static void Main() {
       Console.Beep(200, 300);
   }
}


Console.BufferHeight

 
using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.BufferWidth

 
using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.Clear()

 
using System;
public class Test{
   static void Main(string[] args){
      // Change the Console appearance and redisplay.
      Console.Title = "Cleared / Colored Console";
      Console.ForegroundColor = ConsoleColor.Blue;
      Console.BackgroundColor = ConsoleColor.Yellow;
      Console.Clear();
      Console.WriteLine("Press Enter to change the Console"s appearance.");
      Console.ReadLine();
  }
}


Console.CursorLeft

 
using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.CursorSize

 
using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.CursorTop

 
using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.CursorVisible

 
using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.ForegroundColor

 
using System;
public class Test{
   static void Main(string[] args){
      Console.Title = "Standard Console";
      Console.ForegroundColor = ConsoleColor.Red;
      Console.BackgroundColor = ConsoleColor.Green;
      Console.WriteLine("Press Enter to change the Console"s appearance.");
      Console.ReadLine();
  }
}


Console.In

   
 
using System;  
 
class MainClass { 
  public static void Main() { 
    string str; 
 
    Console.WriteLine("Enter some characters."); 
 
    str = Console.In.ReadLine(); // call TextReader"s ReadLine() method 
 
    Console.WriteLine("You entered: " + str); 
  } 
}


Console.Out

    
 
using System; 
 
class MainClass { 
  public static void Main() { 
    int a=10, b=0; 
    int result; 
 
    Console.Out.WriteLine("This will generate an exception."); 
    try { 
      result = a / b; // generate an exception 
    } catch(DivideByZeroException exc) { 
      Console.Error.WriteLine(exc.Message); 
    } 
  } 
}


Console.Read()

   
 
using System; 
 
class MainClass {   
  public static void Main() { 
    char ch; 
 
    Console.Write("Press a key followed by ENTER: "); 
 
    ch = (char) Console.Read(); // get a char 
    
    Console.WriteLine("Your key is: " + ch); 
  }   
}


Console.ReadLine()

    
using System;
using System.IO;
class MainClass
{
  public static void Main(string[] args)
  {
    string ans;
    do
    {
      Console.Write("Are you done? [yes] [no] : ");
      ans = Console.ReadLine();
    }while(ans != "yes");
  }
}


Console.ResetColor()

 

using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.SetOut

  
using System;  
using System.IO; 
  
public class Redirect { 
  public static void Main() { 
    StreamWriter log_out; 
 
    try { 
      log_out = new StreamWriter("logfile.txt"); 
    } 
    catch(IOException exc) { 
      Console.WriteLine(exc.Message + "Cannot open file."); 
      return ; 
    } 
    
    Console.SetOut(log_out); 
    Console.WriteLine("This is the start of the log file."); 
 
    for(int i=0; i<10; i++) Console.WriteLine(i); 
 
    Console.WriteLine("This is the end of the log file."); 
    log_out.Close(); 
  } 
}


Console.SetWindowSize

 

using System;
public class Test{
   static void Main(string[] args){

      // Change the Console appearance and redisplay.
      Console.Title = "Resized Console";
      Console.ResetColor();
      Console.Clear();
      Console.SetWindowSize(100, 50);
      Console.BufferHeight = 500;
      Console.BufferWidth = 100;
      Console.CursorLeft = 20;
      Console.CursorSize = 50;
      Console.CursorTop = 20;
      Console.CursorVisible = false;
      Console.WriteLine("Main method complete. Press Enter.");
      Console.ReadLine();
  }
}


Console.Title

 
using System;
public class Test{
   static void Main(string[] args){
      Console.Title = "Standard Console";
      Console.ForegroundColor = ConsoleColor.Red;
      Console.BackgroundColor = ConsoleColor.Green;
      Console.WriteLine("Press Enter to change the Console"s appearance.");
      Console.ReadLine();
  }
}


Console.Write

    

class MainClass {
   static void Main()
   {
         System.Console.Write("This is text 1.");
         System.Console.Write("This is text 2.");
         System.Console.Write("This is text 3.");
   }
}


Console.WriteLine

    

using System;
class MainClass
{
   static void Main()
   {
      Console.WriteLine(" {0} and {1}.", 3, 6);
   }
}