Skip to content

Reorganize VB snippets in CLR directory #11544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 12 additions & 12 deletions snippets/visualbasic/Microsoft.Win32/Registry/Overview/source.vb
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
' <Snippet1>
Imports Microsoft.Win32

Class Reg

Public Shared Sub Main()

' Create a RegistryKey, which will access the HKEY_USERS
' key in the registry of this machine.
Dim rk As RegistryKey = Registry.Users

' Print out the keys.
PrintKeys(rk)
End Sub
End Sub

Shared Sub PrintKeys(rkey As RegistryKey)

' Retrieve all the subkeys for the specified key.
Dim names As String() = rkey.GetSubKeyNames()

Dim icount As Integer = 0

Console.WriteLine("Subkeys of " & rkey.Name)
Console.WriteLine("-----------------------------------------------")

' Print the contents of the array to the console.
Dim s As String
For Each s In names
Console.WriteLine(s)

' The following code puts a limit on the number
' of keys displayed. Comment it out to print the
' complete list.
icount += 1
icount += 1
If icount >= 10 Then
Exit For
End If
Next s
End Sub
End Class
' </Snippet1>
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'<Snippet1>
Imports Microsoft.Win32

Expand All @@ -24,7 +24,7 @@
Registry.SetValue(keyName, "TestExpand2", "My path: %path%", _
RegistryValueKind.ExpandString)

' Arrays of strings are stored automatically as
' Arrays of strings are stored automatically as
' MultiString. Similarly, arrays of Byte are stored
' automatically as Binary.
Dim strings() As String = {"One", "Two", "Three"}
Expand All @@ -37,7 +37,7 @@
"Return this default if NoSuchName does not exist.")
Console.WriteLine(vbCrLf & "NoSuchName: {0}", noSuch)

' Retrieve the Integer and Long values, specifying
' Retrieve the Integer and Long values, specifying
' numeric default values in case the name/value pairs
' do not exist. The Integer value is retrieved from the
' default (nameless) name/value pair for the key.
Expand Down Expand Up @@ -93,4 +93,4 @@
'
'Use the registry editor to examine the key.
'Press the Enter key to delete the key.
'</Snippet1>
'</Snippet1>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Public Class RegGetDef
Dim Def As String = rk.GetValue("notavalue", "The default to return")
Console.WriteLine()
Console.WriteLine(def)

rk.Close()
End Sub
End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ Public Class Example
' Delete and recreate the test key.
Registry.CurrentUser.DeleteSubKey("RegistryValueKindExample", False)
Dim rk As RegistryKey = Registry.CurrentUser.CreateSubKey("RegistryValueKindExample")

' Create name/value pairs.
' This overload supports QWord (long) values.
' This overload supports QWord (long) values.
rk.SetValue("QuadWordValue", 42, RegistryValueKind.QWord)

' The following SetValue calls have the same effect as using the
' SetValue overload that does not specify RegistryValueKind.
'
rk.SetValue("DWordValue", 42, RegistryValueKind.DWord)
rk.SetValue("MultipleStringValue", New String() {"One", "Two", "Three"}, RegistryValueKind.MultiString)
rk.SetValue("BinaryValue", New Byte() {10, 43, 44, 45, 14, 255}, RegistryValueKind.Binary)
rk.SetValue("StringValue", "The path is %PATH%", RegistryValueKind.String)
rk.SetValue("StringValue", "The path is %PATH%", RegistryValueKind.String)

' This overload supports setting expandable string values. Compare
' the output from this value with the previous string value.
rk.SetValue("ExpandedStringValue", "The path is %PATH%", RegistryValueKind.ExpandString)


' Display all name/value pairs stored in the test key, with each
' registry data type in parentheses.
'
Expand All @@ -40,7 +40,7 @@ Public Class Example
Console.Write(" ""{0}""", values(i))
Next i
Console.WriteLine()

Case RegistryValueKind.Binary
Dim bytes As Byte() = CType(rk.GetValue(s), Byte())
Console.Write(vbCrLf & " {0} ({1}) =", s, rvk)
Expand All @@ -49,7 +49,7 @@ Public Class Example
Console.Write(" {0:X2}", bytes(i))
Next i
Console.WriteLine()

Case Else
Console.WriteLine(vbCrLf & " {0} ({1}) = {2}", s, rvk, rk.GetValue(s))
End Select
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net481</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Reference Include="System.AddIn" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
' <Snippet1>

Imports System.AddIn.Hosting
Imports System.Collections.ObjectModel

Namespace MathHost
Class Program

Public Shared Sub Main()

' <Snippet2>
' Get the path for the pipeline root.
' Assumes that the current directory is the
' pipline directory structure root directory.
Dim pipeRoot As String = Environment.CurrentDirectory
' <Snippet3>
' Update the cache files of the
' pipeline segments and add-ins.
Dim warnings() As String = AddInStore.Update(pipeRoot)
For Each warning As String In warnings
Console.WriteLine(warning)
Next

' </Snippet3>
' <Snippet4>
' Search for add-ins of type Calculator (the host view of the add-in)
' specifying the host's application base, instead of a path,
' for the FindAddIns method.
Dim tokens As Collection(Of AddInToken) =
AddInStore.FindAddIns(GetType(Calculator), PipelineStoreLocation.ApplicationBase)
' </Snippet4>
' </Snippet2>

' <Snippet5>
'Ask the user which add-in they would like to use.
Dim selectedToken As AddInToken = ChooseAddIn(tokens)
'Activate the selected AddInToken in a new
'application domain with the Internet trust level.
Dim CalcAddIn As Calculator = selectedToken.Activate(Of Calculator)(AddInSecurityLevel.Internet)
'Run the add-in using a custom method.
RunCalculator(CalcAddIn)
' </Snippet5>

' <Snippet6>
' Find a specific add-in.
' Construct the path to the add-in.
Dim addInFilePath As String = (pipeRoot + "\AddIns\P3AddIn2\P3AddIn2.dll")
' The fourth parameter, addinTypeName, takes the full name
' of the type qualified by its namespace. Same as AddInToken.AddInFullName.
Dim tokenColl As System.Collections.ObjectModel.Collection(Of AddInToken) = AddInStore.FindAddIn(GetType(Calculator), pipeRoot, addInFilePath, "CalcAddIns.P3AddIn2")
Console.WriteLine("Found {0}", tokenColl(0).Name)
' </Snippet6>

' <Snippet8>
' Get the AddInController of a
' currently activated add-in (CalcAddIn).
Dim aiController As AddInController = AddInController.GetAddInController(CalcAddIn)

' Select another token.
Dim selectedToken2 As AddInToken = ChooseAddIn(tokens)

' Activate a second add-in, CalcAddIn2, in the same
' appliation domain and process as the first add-in by passing
' the first add-in's AddInEnvironment object to the Activate method.

Dim aiEnvironment As AddInEnvironment = aiController.AddInEnvironment
Dim CalcAddIn2 As Calculator =
selectedToken2.Activate(Of Calculator)(aiEnvironment)

' Get the AddInController for the second add-in to compare environments.
Dim aiController2 As AddInController = AddInController.GetAddInController(CalcAddIn2)

Console.WriteLine("Add-ins in same application domain: {0}",
aiController.AppDomain.Equals(aiController2.AppDomain))
Console.WriteLine("Add-ins in same process: {0}",
aiEnvironment.Process.Equals(aiController2.AddInEnvironment.Process))
' </Snippet8>

'<Snippet9>
' Get the application domain
' of an existing add-in (CalcAddIn).

Dim aiCtrl As AddInController = AddInController.GetAddInController(CalcAddIn)
Dim AddInAppDom As AppDomain = aiCtrl.AppDomain

' Activate another add-in in the same appliation domain.
Dim CalcAddIn3 As Calculator = selectedToken2.Activate(Of Calculator)(AddInAppDom)

' Show that the CalcAddIn3 was loaded
' into CalcCaddIn's application domain.
Dim aic As AddInController = AddInController.GetAddInController(CalcAddIn3)
Console.WriteLine("Add-in loaded into existing application domain: {0}",
aic.AppDomain.Equals(AddInAppDom))
' </Snippet9>

' <Snippet10>
' Create an external process.
Dim pExternal As New AddInProcess()

' Activate an add-in in the external process
' with a full trust security level.
Dim CalcAddIn4 As Calculator =
selectedToken.Activate(Of Calculator)(pExternal,
AddInSecurityLevel.FullTrust)

' Show that the add-in is an external process
' by verifying that it is not in the current (host's) process.
Dim AddinCtl As AddInController = AddInController.GetAddInController(CalcAddIn4)
Console.WriteLine("Add-in in host's process: {0}",
AddinCtl.AddInEnvironment.Process.IsCurrentProcess)
' </Snippet10>

' <Snippet11>
' Use qualification data to control
' how an add-in should be activated.

If selectedToken.QualificationData(AddInSegmentType.AddIn)("Isolation").Equals("NewProcess") Then
' Create an external process.
Dim external As AddInProcess = New AddInProcess

' Activate an add-in in an automatically generated
' application domain with a full trust security level.
Dim CalcAddin5 As Calculator =
selectedToken.Activate(Of Calculator)(external,
AddInSecurityLevel.FullTrust)
Console.WriteLine("Add-in activated per qualification data.")
Else
Console.WriteLine("This add-in is not designated to be activated in a new process.")
End If
' </Snippet11>

' <Snippet12>
' Show the qualification data for each
' token in an AddInToken collection.
For Each token As AddInToken In tokens
For Each qdi As QualificationDataItem In token
Console.WriteLine("{0} {1}\n\t QD Name: {2}, QD Value: {3}",
token.Name, qdi.Segment, qdi.Name, qdi.Value)
Next
Next
' </Snippet12>

End Sub
' <Snippet13>
' Method to select a token by
' enumeratng the AddInToken collection.

Private Shared Function ChooseAddIn(ByVal tokens As System.Collections.ObjectModel.Collection(Of AddInToken)) As AddInToken
If (tokens.Count = 0) Then
Console.WriteLine("No add-ins are available")
Return Nothing
End If
Console.WriteLine("Available add-ins: ")
' <Snippet7>
' Show the token properties for each token
' in the AddInToken collection (tokens),
' preceded by the add-in number in [] brackets.

Dim tokNumber As Integer = 1
For Each tok As AddInToken In tokens
Console.WriteLine(vbTab & "{0}: {1} - {2}" &
vbLf & vbTab & "{3}" &
vbLf & vbTab & "{4}" &
vbLf & vbTab & "{5} - {6}",
tokNumber.ToString, tok.Name,
tok.AddInFullName, tok.AssemblyName,
tok.Description, tok.Version, tok.Publisher)
tokNumber = tokNumber + 1
Next
' </Snippet7>
Console.WriteLine("Which calculator do you want to use?")
Dim line As String = Console.ReadLine
Dim selection As Integer
If Int32.TryParse(line, selection) Then
If (selection <= tokens.Count) Then
Return tokens((selection - 1))
End If
End If
Console.WriteLine("Invalid selection: {0}. Please choose again.", line)
Return ChooseAddIn(tokens)

End Function
'</Snippet13>

Private Shared Sub RunCalculator(ByVal calc As Calculator)
If IsNothing(calc) Then
'No calculators were found, read a line and exit.
Console.ReadLine()
End If
Console.WriteLine(("Available operations: " + calc.Operations))
Console.WriteLine("Type 'exit' to exit")
Dim line As String = Console.ReadLine

While Not line.Equals("exit")
' The Parser class parses the user's input.
Try
Dim c As Parser = New Parser(line)
Console.WriteLine(calc.Operate(c.Action, c.A, c.B))
Catch Ex As System.Exception
Console.WriteLine("Invalid command: {0}. Commands must be formatted: [number] [operation] [number]", line)
Console.WriteLine(("Available operations: " + calc.Operations))
End Try
line = Console.ReadLine

End While
End Sub
End Class

Class Parser

Private partA As Double

Private partB As Double

Private act As String

Friend Sub New(ByVal line As String)
MyBase.New()
Dim parts() As String = line.Trim.Split(" ")
partA = Double.Parse(parts(0))
act = parts(1)
partB = Double.Parse(parts(2))
End Sub

Public ReadOnly Property A() As Double
Get
Return partA
End Get
End Property

Public ReadOnly Property B() As Double
Get
Return partB
End Get
End Property

Public ReadOnly Property Action() As String
Get
Return act
End Get
End Property
End Class
End Namespace
' </Snippet1>

Loading