.NET: Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
Zeile 29: Zeile 29:
 
The value 3 means "DRIVE_FIXED".<br>
 
The value 3 means "DRIVE_FIXED".<br>
 
More informations in the documentation: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdrivetypew
 
More informations in the documentation: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdrivetypew
 +
 +
 +
 +
 +
 +
== Troubleshooting ==
 +
=== Error "StringBuilder" ===
 +
'''The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)'''
 +
 +
==== Description ====
 +
''Indeed we are. StringBuilder is defined in the System.Text namespace, but the using directive goes at the top of the program by the class definition. Since we’re letting PowerShell define the type for us, we can either rename it to System.Text.StringBuilder,''
 +
 +
==== Solution ====
 +
Replace '''StringBuilder''' with '''System.Text.StringBuilder'''.
  
  

Version vom 14. April 2020, 13:33 Uhr

Get all actual loaded .NET Assemblies (per PowerShell Session)

[System.AppDomain]::CurrentDomain.GetAssemblies()


Messagebox with Button

[System.Windows.Forms.MessageBox]::Show("Text","Überschrift",[System.Windows.Forms.MessageBoxButtons]::OK)


Call a Win32 API function with powershell

Example: GetDriveTypeW

Using Add-Type to call the GetDriveTypeW function

$MethodDefinition = @'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern int GetDriveTypeW(string lpRootPathName);
'@
 
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru

Call the function with the paramter "C:\"

$Kernel32::GetDriveTypeW('C:\')

Output

3

Description The value 3 means "DRIVE_FIXED".
More informations in the documentation: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdrivetypew



Troubleshooting

Error "StringBuilder"

The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)

Description

Indeed we are. StringBuilder is defined in the System.Text namespace, but the using directive goes at the top of the program by the class definition. Since we’re letting PowerShell define the type for us, we can either rename it to System.Text.StringBuilder,

Solution

Replace StringBuilder with System.Text.StringBuilder.




More informations: