Export (0) Print
Expand All

SecureString Constructor

Initializes a new instance of the SecureString class.

Namespace:  System.Security
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Sub New

ExceptionCondition
CryptographicException

An error occurred while encrypting or decrypting the value of this instance.

NotSupportedException

This operation is not supported on this platform.

Windows 2000 Platform Note: SecureString is only supported on Windows 2000 Service Pack 3 and later.

The following example uses the default (or parameterless) constructor to instantiate a new SecureString object. It then calls the AppendChar method to add an array of characters to it.

Imports System.Security

Module Example
   Public Sub Main()
      ' Define the string value to assign to a new secure string. 
      Dim chars() As Char = { "t"c, "e"c, "s"c, "t"c }
      ' Instantiate the secure string. 
      Dim testString As SecureString = New SecureString()
      ' Assign the character array to the secure string. 
      For Each ch As char In chars
         testString.AppendChar(ch)
      Next          
      ' Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", _ 
                        testString.Length)
   End Sub 
End Module 
' The example displays the following output: 
'      The length of the string is 4 characters.

The following example creates a SecureString object from the value of a String object.

Imports System.Security

Module Example
   Public Sub Main()
      ' Define the string value to be assigned to the secure string. 
      Dim initString As String = "TestString" 
      ' Instantiate the secure string. 
      Dim testString As SecureString = New SecureString()
      ' Use the AppendChar method to add each char value to the secure string. 
      For Each ch As Char In initString
         testString.AppendChar(ch)
      Next    
      ' Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", _ 
                        testString.Length)
   End Sub 
End Module 
' The example displays the following output: 
'      The length of the string is 10 characters.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft