.NET: Unterschied zwischen den Versionen
Admin (Diskussion | Beiträge) |
Admin (Diskussion | Beiträge) |
||
(2 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt) | |||
Zeile 32: | Zeile 32: | ||
− | + | === Example-Function: GetLogicalDriveStrings (running C# in PowerShell) === | |
− | + | ||
− | === | + | |
<source lang="powershell"> | <source lang="powershell"> | ||
$code = @" | $code = @" | ||
Zeile 80: | Zeile 78: | ||
[Mainline]::main() | [Mainline]::main() | ||
</source> | </source> | ||
+ | The code is a copy of the function from PInvoke: http://pinvoke.net/default.aspx/kernel32/GetLogicalDriveStrings.html | ||
+ | |||
+ | |||
+ | === Example-Functiom: DriveInfo.TotalSize (.NET Framework 4.8) === | ||
+ | <source lang="powershell"> | ||
+ | $code = @" | ||
+ | using System; | ||
+ | using System.IO; | ||
+ | |||
+ | public class TestGetDrives | ||
+ | { | ||
+ | public static void Main() | ||
+ | { | ||
+ | DriveInfo[] allDrives = DriveInfo.GetDrives(); | ||
+ | |||
+ | foreach (DriveInfo d in allDrives) | ||
+ | { | ||
+ | Console.WriteLine("Drive {0}", d.Name); | ||
+ | Console.WriteLine(" Drive type: {0}", d.DriveType); | ||
+ | if (d.IsReady == true) | ||
+ | { | ||
+ | Console.WriteLine(" Volume label: {0}", d.VolumeLabel); | ||
+ | Console.WriteLine(" File system: {0}", d.DriveFormat); | ||
+ | Console.WriteLine( | ||
+ | " Available space to current user:{0, 15} bytes", | ||
+ | d.AvailableFreeSpace); | ||
+ | |||
+ | Console.WriteLine( | ||
+ | " Total available space: {0, 15} bytes", | ||
+ | d.TotalFreeSpace); | ||
+ | |||
+ | Console.WriteLine( | ||
+ | " Total size of drive: {0, 15} bytes ", | ||
+ | d.TotalSize); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | "@ | ||
+ | |||
+ | Add-Type -TypeDefinition $code -Language CSharp | ||
+ | [TestGetDrives]::main() | ||
+ | </source> | ||
+ | Source: https://docs.microsoft.com/en-us/dotnet/api/system.io.driveinfo.totalsize?view=netframework-4.8 | ||
Zeile 103: | Zeile 145: | ||
More informations: | More informations: | ||
*https://devblogs.microsoft.com/scripting/use-powershell-to-interact-with-the-windows-api-part-1/ | *https://devblogs.microsoft.com/scripting/use-powershell-to-interact-with-the-windows-api-part-1/ | ||
+ | *https://blog.adamfurmanek.pl/2016/03/19/executing-c-code-using-powershell-script/ | ||
[[Kategorie:PowerShell]] | [[Kategorie:PowerShell]] |
Aktuelle Version vom 15. April 2020, 12:03 Uhr
Inhaltsverzeichnis
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-Function: 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
Example-Function: GetLogicalDriveStrings (running C# in PowerShell)
$code = @" using System; using System.Collections.Specialized; using System.Runtime.InteropServices; public class Mainline { [DllImport("kernel32.dll")] static extern uint GetLogicalDriveStrings(uint nBufferLength, [Out] char[] lpBuffer); public static void Main() { const int size = 512; char[] buffer = new char[size]; uint code = GetLogicalDriveStrings(size, buffer); if (code == 0) { Console.WriteLine("Call failed"); return; } StringCollection list = new StringCollection(); int start = 0; for (int i = 0; i < code; ++i) { if (buffer[i] == 0) { string s = new string(buffer, start, i - start); list.Add(s); start = i + 1; } } foreach (string s in list) Console.WriteLine(s); } } "@ Add-Type -TypeDefinition $code -Language CSharp [Mainline]::main()
The code is a copy of the function from PInvoke: http://pinvoke.net/default.aspx/kernel32/GetLogicalDriveStrings.html
Example-Functiom: DriveInfo.TotalSize (.NET Framework 4.8)
$code = @" using System; using System.IO; public class TestGetDrives { public static void Main() { DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { Console.WriteLine("Drive {0}", d.Name); Console.WriteLine(" Drive type: {0}", d.DriveType); if (d.IsReady == true) { Console.WriteLine(" Volume label: {0}", d.VolumeLabel); Console.WriteLine(" File system: {0}", d.DriveFormat); Console.WriteLine( " Available space to current user:{0, 15} bytes", d.AvailableFreeSpace); Console.WriteLine( " Total available space: {0, 15} bytes", d.TotalFreeSpace); Console.WriteLine( " Total size of drive: {0, 15} bytes ", d.TotalSize); } } } } "@ Add-Type -TypeDefinition $code -Language CSharp [TestGetDrives]::main()
Source: https://docs.microsoft.com/en-us/dotnet/api/system.io.driveinfo.totalsize?view=netframework-4.8
Troubleshooting
Error "StringBuilder"
The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)
Description
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: