当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Console类代码示例

本文整理汇总了VB.NET中System.Console的典型用法代码示例。如果您正苦于以下问题:VB.NET Console类的具体用法?VB.NET Console怎么用?VB.NET Console使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Console类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Example

Public Class Example
    Public Shared Sub Main()
        Console.Write("Hello ")
        Console.WriteLine("World!")
        Console.Write("Enter your name: ")
        Dim name As String = Console.ReadLine()
        Console.Write("Good day, ")
        Console.Write(name)
        Console.WriteLine("!")
    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:11,代码来源:Console

输出:

Hello World!
Enter your name: James
Good day, James!

示例2: Example

Module Example
   Public Sub Main()
      ' Create a Char array for the modern Cyrillic alphabet, 
      ' from U+0410 to U+044F.
      Dim nChars As Integer = &h44F - &h0410
      Dim chars(nChars) As Char
      Dim codePoint As UInt16 = &h0410
      For ctr As Integer = 0 To chars.Length - 1
        chars(ctr) = ChrW(codePoint)
        codePoint += CType(1, UShort)
      Next   
         
      Console.WriteLine("Current code page: {0}", 
                        Console.OutputEncoding.CodePage)
      Console.WriteLine()
      ' Display the characters.
      For Each ch In chars
         Console.Write("{0}  ", ch)
         If Console.CursorLeft >= 70 Then Console.WriteLine()
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:22,代码来源:Console

输出:

Current code page: 437

?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?
?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?
?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?

示例3: Example

' 导入命名空间
Imports System.Runtime.InteropServices

Public Module Example
   ' <DllImport("kernel32.dll", SetLastError = true)>
   Private Declare Function GetStdHandle Lib "Kernel32" (
                   nStdHandle As Integer) As IntPtr

   ' [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
   Private Declare Function GetCurrentConsoleFontEx Lib "Kernel32" ( 
                   consoleOutput As IntPtr, 
                   maximumWindow As Boolean,
                   ByRef lpConsoleCurrentFontEx As CONSOLE_FONT_INFO_EX) As Boolean
          
   ' [DllImport("kernel32.dll", SetLastError = true)]
   Private Declare Function SetCurrentConsoleFontEx Lib "Kernel32"(
                   consoleOutput As IntPtr, 
                   maximumWindow As Boolean,
                   consoleCurrentFontEx As CONSOLE_FONT_INFO_EX) As Boolean
   
   Private Const STD_OUTPUT_HANDLE As Integer = -11
   Private Const TMPF_TRUETYPE As Integer = 4
   Private Const LF_FACESIZE As Integer= 32
   Private INVALID_HANDLE_VALUE As IntPtr = New IntPtr(-1)
   
   Public Sub Main()
      Dim fontName As String = "Lucida Console"
      Dim hnd As IntPtr = GetStdHandle(STD_OUTPUT_HANDLE)
      If hnd <> INVALID_HANDLE_VALUE Then
         Dim info AS CONSOLE_FONT_INFO_EX = New CONSOLE_FONT_INFO_EX()
         info.cbSize = CUInt(Marshal.SizeOf(info))
         Dim tt As Boolean = False
         ' First determine whether there's already a TrueType font.
         If GetCurrentConsoleFontEx(hnd, False, info) Then
            tt = (info.FontFamily And TMPF_TRUETYPE) = TMPF_TRUETYPE
            If tt Then
               Console.WriteLine("The console already is using a TrueType font.")
               Return
            End If
            ' Set console font to Lucida Console.
            Dim newInfo As CONSOLE_FONT_INFO_EX = New CONSOLE_FONT_INFO_EX()
            newInfo.cbSize = CUInt(Marshal.SizeOf(newInfo))          
            newInfo.FontFamily = TMPF_TRUETYPE
            newInfo.FaceName = fontName
            ' Get some settings from current font.
            newInfo.dwFontSize = New COORD(info.dwFontSize.X, info.dwFontSize.Y)
            newInfo.FontWeight = info.FontWeight
            SetCurrentConsoleFontEx(hnd, False, newInfo)
         End If
      End If    
   End Sub
 
   <StructLayout(LayoutKind.Sequential)> Friend Structure COORD
      Friend X As Short
      Friend Y As Short
      
      Friend Sub New(x As Short, y As Short)
         Me.X = x
         Me.Y = y
      End Sub
   End Structure
 
   <StructLayout(LayoutKind.Sequential, CharSet := CharSet.Unicode)> Friend Structure CONSOLE_FONT_INFO_EX 
      Friend cbSize As UInteger
      Friend nFont As UInteger
      Friend dwFontSize As COORD
      Friend FontFamily As Integer
      Friend FontWeight As Integer
      <MarshalAs(UnmanagedType.ByValTStr, SizeConst := 32)> Friend FaceName As String
   End Structure 
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:71,代码来源:Console

示例4: Example

' 导入命名空间
Imports Microsoft.Win32

Module Example
   Public Sub Main()
      Dim valueName As String = "Lucida Console"
      Dim newFont As String = "simsun.ttc,SimSun"
      Dim fonts() As String = Nothing
      Dim kind As RegistryValueKind 
      Dim toAdd As Boolean
      
      Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey( 
                 "Software\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink", 
                 True)
      If key Is Nothing Then
         Console.WriteLine("Font linking is not enabled.")
      Else
         ' Determine if the font is a base font.
         Dim names() As String = key.GetValueNames()
         If Array.Exists(names, Function(s) s.Equals(valueName, 
                                                     StringComparison.OrdinalIgnoreCase))
            ' Get the value's type.
            kind = key.GetValueKind(valueName)

            ' Type should be RegistryValueKind.MultiString, but we can't be sure.
            Select Case kind
               Case RegistryValueKind.String
                  fonts = { CStr(key.GetValue(valueName)) }   
               Case RegistryValueKind.MultiString
                  fonts = CType(key.GetValue(valueName), String())
               Case RegistryValueKind.None
                  ' Do nothing.
                  fonts = { }
            End Select 
            ' Determine whether SimSun is a linked font.
            If Array.FindIndex(fonts, Function(s) s.IndexOf("SimSun", 
                                      StringComparison.OrdinalIgnoreCase) >=0) >= 0 Then
               Console.WriteLine("Font is already linked.")
               toAdd = False
            Else
               ' Font is not a linked font.
               toAdd = True
            End If
         Else
            ' Font is not a base font.
            toAdd = True
            fonts = { }
         End If

         If toAdd Then  
            Array.Resize(fonts, fonts.Length + 1)
            fonts(fonts.GetUpperBound(0)) = newFont
            ' Change REG_SZ to REG_MULTI_SZ.
            If kind = RegistryValueKind.String Then
               key.DeleteValue(valueName, False)
            End If
            key.SetValue(valueName, fonts, RegistryValueKind.MultiString)
            Console.WriteLine("SimSun added to the list of linked fonts.")
         End If                     
      End If
      
      If key IsNot Nothing Then key.Close()
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:64,代码来源:Console

示例5: Example

Module Example
   Public Sub Main()
      Dim chars() As Char = { ChrW(&h0061), ChrW(&h0308) }
   
      Dim combining As String = New String(chars)
      Console.WriteLine(combining)
      
      combining = combining.Normalize()
      Console.WriteLine(combining)
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:11,代码来源:Console

输出:

a"
ä

示例6: DisplayChars

' 导入命名空间
Imports System.IO
Imports System.Globalization
Imports System.Text

Public Module DisplayChars
   Public Sub Main(args() As String)
      Dim rangeStart As UInteger = 0
      Dim rangeEnd As UInteger = 0
      Dim setOutputEncodingToUnicode As Boolean = True
      ' Get the current encoding so we can restore it.
      Dim originalOutputEncoding As Encoding = Console.OutputEncoding

    Try
         Select Case args.Length
            Case 2
               rangeStart = UInt32.Parse(args(0), NumberStyles.HexNumber)
               rangeEnd = UInt32.Parse(args(1), NumberStyles.HexNumber)
               setOutputEncodingToUnicode = True
            Case 3
               If Not UInt32.TryParse(args(0), NumberStyles.HexNumber, Nothing, rangeStart) Then
                  Throw New ArgumentException(String.Format("{0} is not a valid hexadecimal number.", args(0)))
               End If
               
               If Not UInt32.TryParse(args(1), NumberStyles.HexNumber, Nothing, rangeEnd) Then
                  Throw New ArgumentException(String.Format("{0} is not a valid hexadecimal number.", args(1)))
               End If
               
               Boolean.TryParse(args(2), setOutputEncodingToUnicode)
            Case Else
               Console.WriteLine("Usage: {0} <{1}> <{2}> [{3}]", 
                                 Environment.GetCommandLineArgs()(0), 
                                 "startingCodePointInHex", 
                                 "endingCodePointInHex", 
                                 "<setOutputEncodingToUnicode?{true|false, default:false}>")
               Exit Sub
         End Select
   
         If setOutputEncodingToUnicode Then
            ' This won't work before .NET Framework 4.5.
            Try 
               ' Set encoding Imports endianness of this system.
               ' We're interested in displaying individual Char objects, so 
               ' we don't want a Unicode BOM or exceptions to be thrown on
               ' invalid Char values.
               Console.OutputEncoding = New UnicodeEncoding(Not BitConverter.IsLittleEndian, False) 
               Console.WriteLine("{0}Output encoding set to UTF-16", vbCrLf)
            Catch e As IOException
               Console.OutputEncoding = New UTF8Encoding()
               Console.WriteLine("Output encoding set to UTF-8")
            End Try
         Else
            Console.WriteLine("The console encoding is {0} (code page {1})", 
                              Console.OutputEncoding.EncodingName,
                              Console.OutputEncoding.CodePage)
         End If
         DisplayRange(rangeStart, rangeEnd)
      Catch ex As ArgumentException
         Console.WriteLine(ex.Message)
      Finally
         ' Restore console environment.
         Console.OutputEncoding = originalOutputEncoding
      End Try
   End Sub

   Public Sub DisplayRange(rangeStart As UInteger, rangeEnd As UInteger)
      Const upperRange As UInteger = &h10FFFF
      Const surrogateStart As UInteger = &hD800
      Const surrogateEnd As UInteger = &hDFFF
       
      If rangeEnd <= rangeStart Then
         Dim t As UInteger = rangeStart
         rangeStart = rangeEnd
         rangeEnd = t
      End If

      ' Check whether the start or end range is outside of last plane.
      If rangeStart > upperRange Then
         Throw New ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{1:X5})",
                                                   rangeStart, upperRange))                                   
      End If
      If rangeEnd > upperRange Then
         Throw New ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{0:X5})",
                                                   rangeEnd, upperRange))
      End If
      ' Since we're using 21-bit code points, we can't use U+D800 to U+DFFF.
      If (rangeStart < surrogateStart And rangeEnd > surrogateStart) OrElse (rangeStart >= surrogateStart And rangeStart <= surrogateEnd )
         Throw New ArgumentException(String.Format("0x{0:X5}-0x{1:X5} includes the surrogate pair range 0x{2:X5}-0x{3:X5}", 
                                                   rangeStart, rangeEnd, surrogateStart, surrogateEnd))         
      End If
      
      Dim last As UInteger = RoundUpToMultipleOf(&h10, rangeEnd)
      Dim first As UInteger = RoundDownToMultipleOf(&h10, rangeStart)

      Dim rows As UInteger = (last - first) \ &h10

      For r As UInteger = 0 To rows - 1
         ' Display the row header.
         Console.Write("{0:x5} ", first + &h10 * r)

         For c As UInteger = 1 To &h10
            Dim cur As UInteger = first + &h10 * r + c
            If cur  < rangeStart Then
               Console.Write(" {0} ", Convert.ToChar(&h20))
            Else If rangeEnd < cur Then
               Console.Write(" {0} ", Convert.ToChar(&h20))
            Else 
               ' the cast to int is safe, since we know that val <= upperRange.
               Dim chars As String = Char.ConvertFromUtf32(CInt(cur))
               ' Display a space for code points that are not valid characters.
               If CharUnicodeInfo.GetUnicodeCategory(chars(0)) = 
                                   UnicodeCategory.OtherNotAssigned Then
                  Console.Write(" {0} ", Convert.ToChar(&h20))
               ' Display a space for code points in the private use area.
               Else If CharUnicodeInfo.GetUnicodeCategory(chars(0)) =
                                        UnicodeCategory.PrivateUse Then
                 Console.Write(" {0} ", Convert.ToChar(&h20))
               ' Is surrogate pair a valid character?
               ' Note that the console will interpret the high and low surrogate
               ' as separate (and unrecognizable) characters.
               Else If chars.Length > 1 AndAlso CharUnicodeInfo.GetUnicodeCategory(chars, 0) = 
                                            UnicodeCategory.OtherNotAssigned Then
                  Console.Write(" {0} ", Convert.ToChar(&h20))
               Else
                  Console.Write(" {0} ", chars) 
               End If   
            End If
            
            Select Case c
               Case 3, 11
                  Console.Write("-")
               Case 7
                  Console.Write("--")
            End Select
         Next

         Console.WriteLine()
         If 0 < r AndAlso r Mod &h10 = 0
            Console.WriteLine()
         End If
      Next
   End Sub

   Private Function RoundUpToMultipleOf(b As UInteger, u As UInteger) As UInteger
      Return RoundDownToMultipleOf(b, u) + b
   End Function

   Private Function RoundDownToMultipleOf(b As UInteger, u As UInteger) As UInteger
      Return u - (u Mod b)
   End Function
End Module
' If the example is run with the command line
'       DisplayChars 0400 04FF true
' the example displays the Cyrillic character set as follows:
'       Output encoding set to UTF-16
'       00400  Ѐ  Ё  Ђ  Ѓ - Є  Ѕ  І  Ї -- Ј  Љ  Њ  Ћ - Ќ  Ѝ  Ў  Џ
'       00410  А  Б  В  Г - Д  Е  Ж  З -- И  Й  К  Л - М  Н  О  П
'       00420  Р  С  Т  У - Ф  Х  Ц  Ч -- Ш  Щ  Ъ  Ы - Ь  Э  Ю  Я
'       00430  а  б  в  г - д  е  ж  з -- и  й  к  л - м  н  о  п
'       00440  р  с  т  у - ф  х  ц  ч -- ш  щ  ъ  ы - ь  э  ю  я
'       00450  ѐ  ё  ђ  ѓ - є  ѕ  і  ї -- ј  љ  њ  ћ - ќ  ѝ  ў  џ
'       00460  Ѡ  ѡ  Ѣ  ѣ - Ѥ  ѥ  Ѧ  ѧ -- Ѩ  ѩ  Ѫ  ѫ - Ѭ  ѭ  Ѯ  ѯ
'       00470  Ѱ  ѱ  Ѳ  ѳ - Ѵ  ѵ  Ѷ  ѷ -- Ѹ  ѹ  Ѻ  ѻ - Ѽ  ѽ  Ѿ  ѿ
'       00480  Ҁ  ҁ  ҂  ҃ - ҄  ҅  ҆  ҇ -- ҈  ҉  Ҋ  ҋ - Ҍ  ҍ  Ҏ  ҏ
'       00490  Ґ  ґ  Ғ  ғ - Ҕ  ҕ  Җ  җ -- Ҙ  ҙ  Қ  қ - Ҝ  ҝ  Ҟ  ҟ
'       004a0  Ҡ  ҡ  Ң  ң - Ҥ  ҥ  Ҧ  ҧ -- Ҩ  ҩ  Ҫ  ҫ - Ҭ  ҭ  Ү  ү
'       004b0  Ұ  ұ  Ҳ  ҳ - Ҵ  ҵ  Ҷ  ҷ -- Ҹ  ҹ  Һ  һ - Ҽ  ҽ  Ҿ  ҿ
'       004c0  Ӏ  Ӂ  ӂ  Ӄ - ӄ  Ӆ  ӆ  Ӈ -- ӈ  Ӊ  ӊ  Ӌ - ӌ  Ӎ  ӎ  ӏ
'       004d0  Ӑ  ӑ  Ӓ  ӓ - Ӕ  ӕ  Ӗ  ӗ -- Ә  ә  Ӛ  ӛ - Ӝ  ӝ  Ӟ  ӟ
'       004e0  Ӡ  ӡ  Ӣ  ӣ - Ӥ  ӥ  Ӧ  ӧ -- Ө  ө  Ӫ  ӫ - Ӭ  ӭ  Ӯ  ӯ
'       004f0  Ӱ  ӱ  Ӳ  ӳ - Ӵ  ӵ  Ӷ  ӷ -- Ӹ  ӹ  Ӻ  ӻ - Ӽ  ӽ  Ӿ  ӿ
开发者ID:VB.NET开发者,项目名称:System,代码行数:171,代码来源:Console


注:本文中的System.Console类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。