<#PSScriptInfo
.VERSION 1.0
.GUID 26C67DF9-0F1E-4806-9D23-586DFBDECF88
.AUTHOR ElektroStudios
.COMPANYNAME ElektroStudios
.COPYRIGHT ElektroStudios © 2025
#>
$vbCode = @'
#Region " Option Statements "
Option Strict On
Option Explicit On
Option Infer Off
#End Region
#Region " Imports "
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Diagnostics.CodeAnalysis
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Text
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization
Imports DevCase.Core.IO.DigitalInformation
Imports DevCase.Core.IO.FileSystem
Imports DevCase.Runtime.TypeConverters
Imports DevCase.Win32
Imports DevCase.Win32.Enums
#End Region
#Region " CreateDummyFileOptionsForm "
Public NotInheritable Class CreateDummyFileOptionsForm : Inherits System.Windows.Forms.Form
Public Sub New()
Me.InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Friend WithEvents PropertyGrid1 As New PropertyGrid
Friend WithEvents ButtonCancel As Button
Friend WithEvents ButtonOk As Button
Friend WithEvents TableLayoutPanel1 As TableLayoutPanel
Friend WithEvents Panel1 As Panel
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.PropertyGrid1 = New System.Windows.Forms.PropertyGrid()
Me.ButtonCancel = New System.Windows.Forms.Button()
Me.ButtonOk = New System.Windows.Forms.Button()
Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Me.Panel1 = New System.Windows.Forms.Panel()
Me.TableLayoutPanel1.SuspendLayout()
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'PropertyGrid1
'
Me.PropertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill
Me.PropertyGrid1.Location = New System.Drawing.Point(3, 4)
Me.PropertyGrid1.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
Me.PropertyGrid1.Name = "PropertyGrid1"
Me.PropertyGrid1.PropertySort = System.Windows.Forms.PropertySort.NoSort
Me.PropertyGrid1.Size = New System.Drawing.Size(733, 157)
Me.PropertyGrid1.TabIndex = 0
Me.PropertyGrid1.ToolbarVisible = False
'
'ButtonCancel
'
Me.ButtonCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ButtonCancel.Location = New System.Drawing.Point(624, 6)
Me.ButtonCancel.Name = "ButtonCancel"
Me.ButtonCancel.Size = New System.Drawing.Size(100, 40)
Me.ButtonCancel.TabIndex = 1
Me.ButtonCancel.Text = "Cancel"
Me.ButtonCancel.UseVisualStyleBackColor = True
'
'ButtonOk
'
Me.ButtonOk.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ButtonOk.Location = New System.Drawing.Point(518, 6)
Me.ButtonOk.Name = "ButtonOk"
Me.ButtonOk.Size = New System.Drawing.Size(100, 40)
Me.ButtonOk.TabIndex = 2
Me.ButtonOk.Text = "Ok"
Me.ButtonOk.UseVisualStyleBackColor = True
'
'TableLayoutPanel1
'
Me.TableLayoutPanel1.ColumnCount = 1
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
Me.TableLayoutPanel1.Controls.Add(Me.Panel1, 0, 1)
Me.TableLayoutPanel1.Controls.Add(Me.PropertyGrid1, 0, 0)
Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
Me.TableLayoutPanel1.RowCount = 2
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 73.17073!))
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 26.82927!))
Me.TableLayoutPanel1.Size = New System.Drawing.Size(739, 226)
Me.TableLayoutPanel1.TabIndex = 3
'
'Panel1
'
Me.Panel1.Controls.Add(Me.ButtonOk)
Me.Panel1.Controls.Add(Me.ButtonCancel)
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel1.Location = New System.Drawing.Point(3, 168)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(733, 55)
Me.Panel1.TabIndex = 4
'
'CreateDummyFileOptionsForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 17.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(739, 226)
Me.Controls.Add(Me.TableLayoutPanel1)
Me.Font = New System.Drawing.Font("Segoe UI", 10.0!)
Me.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
Me.MaximizeBox = False
Me.Name = "CreateDummyFileOptionsForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Create Dummy File Options"
Me.TableLayoutPanel1.ResumeLayout(False)
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Public Property DummyFileOptions As New DummyFileOptions
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.PropertyGrid1.SelectedObject = Me.DummyFileOptions
DevCase.Extensions.PropertyGridExtensions.SetSplitterPosition(Me.PropertyGrid1, 145)
Me.MinimumSize = Me.Size
Me.MaximumSize = New Size(3840, Me.Height)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonCancel.Click
Me.DialogResult = DialogResult.Cancel
Me.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles ButtonOk.Click
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
End Class
#End Region
#Region " DummyFileOptions "
Public NotInheritable Class DummyFileOptions
<Browsable(True)>
<Editor(GetType(SaveFileNameEditor), GetType(UITypeEditor))>
<DisplayName("File Path")>
<Description("File path where to create the dummy file.")>
Public Property FilePath As String = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "File.dummy")
<TypeConverter(GetType(FileSizeConverter))>
<DisplayName("File Size")>
<Description("Size of the dummy file.")>
Public Property FileSize As New FileSize(1, DigitalStorageUnits.Megabyte)
<Browsable(False)>
Public ReadOnly Property FileSizeBytes As ULong
Get
Return CULng(Me.FileSize.Size(DigitalStorageUnits.Byte))
End Get
End Property
<TypeConverter(GetType(FileSizeConverter))>
<DisplayName("Buffer Size")>
<Description("Size of the buffer used to write the dummy file.")>
Public Property BufferSize As New FileSize(4, DigitalStorageUnits.Megabyte)
<Browsable(False)>
Public ReadOnly Property BufferSizeBytes As Integer
Get
Return CInt(Me.BufferSize.Size(DigitalStorageUnits.Byte))
End Get
End Property
<DisplayName("Use Random Data")>
<Description("If True, uses random bytes to fill the dummy file content; othewise, fills the dummy file with zero bytes.")>
Public Property UseRandomData As Boolean = False
End Class
#End Region
#Region " SaveFileNameEditor "
Public NotInheritable Class SaveFileNameEditor : Inherits UITypeEditor
Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function
Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
If context Is Nothing OrElse context.Instance Is Nothing OrElse provider Is Nothing Then
Return MyBase.EditValue(context, provider, value)
End If
Using saveFileDialog As New SaveFileDialog()
If value IsNot Nothing Then
saveFileDialog.FileName = value.ToString()
End If
saveFileDialog.Title = "Select a location to save the dummy file"
saveFileDialog.Filter = "All files (*.*)|*.*"
saveFileDialog.FileName = "File.dummy"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
value = saveFileDialog.FileName
End If
End Using
Return value
End Function
End Class
#End Region
#Region " NativeMethods "
Namespace DevCase.Win32.NativeMethods
<HideModuleName>
<SuppressUnmanagedCodeSecurity>
<SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", Justification:="Required to migrate this code to .NET Core")>
<SuppressMessage("Interoperability", "CA1401:P/Invokes should not be visible", Justification:="")>
Public Module ShlwApi
#Region " ShlwApi.dll "
<DllImport("ShlwApi.dll", SetLastError:=True, ExactSpelling:=True, CharSet:=CharSet.Ansi, BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
Public Function StrFormatByteSize64A(number As ULong,
buffer As StringBuilder,
bufferSize As UInteger
) As IntPtr
End Function
<DllImport("ShlwApi.dll", SetLastError:=False, ExactSpelling:=True, CharSet:=CharSet.Unicode)>
Public Function StrFormatByteSizeEx(number As ULong,
flags As StrFormatByteSizeFlags,
buffer As StringBuilder,
bufferSize As UInteger
) As Integer ' HResult
End Function
#End Region
End Module
End Namespace
#End Region
#Region " StrFormatByteSizeFlags "
Namespace DevCase.Win32.Enums
Public Enum StrFormatByteSizeFlags
RoundToNearest = 1
Truncate = 2
End Enum
End Namespace
#End Region
#Region " Size Units "
Namespace DevCase.Core.IO.DigitalInformation
Public Enum DigitalStorageUnits As Long
[Byte] = CLng(2 ^ 0)
Kilobyte = CLng(2 ^ 10)
Megabyte = CLng(2 ^ 20)
Gigabyte = CLng(2 ^ 30)
Terabyte = CLng(2 ^ 40)
Petabyte = CLng(2 ^ 50)
Exabyte = CLng(2 ^ 60)
End Enum
End Namespace
#End Region
#Region " Filesize "
Namespace DevCase.Core.IO.FileSystem
Public NotInheritable Class FileSize
#Region " Private Fields "
Private ReadOnly bytesB As Double
#End Region
#Region " Properties "
Public ReadOnly Property Size(sizeUnit As DigitalStorageUnits) As Double
Get
Return Convert(size:=Me.bytesB, fromUnit:=DigitalStorageUnits.Byte, toUnit:=sizeUnit)
End Get
End Property
Public ReadOnly Property Size(sizeUnit As DigitalStorageUnits, decimalPrecision As Integer, Optional numberFormatInfo As NumberFormatInfo = Nothing) As String
Get
If numberFormatInfo Is Nothing Then
numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
End If
Return Me.Size(sizeUnit).ToString(String.Format("N{0}", decimalPrecision), numberFormatInfo)
End Get
End Property
Public Property SizeRounded As Double
Public ReadOnly Property SizeRounded(decimalPrecision As Integer, Optional numberFormatInfo As NumberFormatInfo = Nothing) As String
Get
If numberFormatInfo Is Nothing Then
numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
End If
Return Me.SizeRounded.ToString(String.Format("N{0}", decimalPrecision), numberFormatInfo)
End Get
End Property
Public Property SizeUnit As DigitalStorageUnits
Public Property SizeUnitNameShort As String
Public Property SizeUnitNameLong As String
#End Region
#Region " Constructors "
Private Sub New()
End Sub
<DebuggerStepThrough>
Public Sub New(size As Double, sizeUnit As DigitalStorageUnits)
Me.bytesB = System.Convert.ToDouble(size * sizeUnit)
Select Case System.Math.Abs(Me.bytesB)
Case Is >= DigitalStorageUnits.Petabyte
Me.SizeRounded = Me.bytesB / DigitalStorageUnits.Petabyte
Me.SizeUnit = DigitalStorageUnits.Petabyte
Me.SizeUnitNameShort = "PB"
Me.SizeUnitNameLong = "PetaBytes"
Case Is >= DigitalStorageUnits.Terabyte
Me.SizeRounded = Me.bytesB / DigitalStorageUnits.Terabyte
Me.SizeUnit = DigitalStorageUnits.Terabyte
Me.SizeUnitNameShort = "TB"
Me.SizeUnitNameLong = "TeraBytes"
Case Is >= DigitalStorageUnits.Gigabyte
Me.SizeRounded = Me.bytesB / DigitalStorageUnits.Gigabyte
Me.SizeUnit = DigitalStorageUnits.Gigabyte
Me.SizeUnitNameShort = "GB"
Me.SizeUnitNameLong = "GigaBytes"
Case Is >= DigitalStorageUnits.Megabyte
Me.SizeRounded = Me.bytesB / DigitalStorageUnits.Megabyte
Me.SizeUnit = DigitalStorageUnits.Megabyte
Me.SizeUnitNameShort = "MB"
Me.SizeUnitNameLong = "MegaBytes"
Case Is >= DigitalStorageUnits.Kilobyte
Me.SizeRounded = Me.bytesB / DigitalStorageUnits.Kilobyte
Me.SizeUnit = DigitalStorageUnits.Kilobyte
Me.SizeUnitNameShort = "KB"
Me.SizeUnitNameLong = "KiloBytes"
Case Is >= DigitalStorageUnits.Byte, Is = 0
Me.SizeRounded = Me.bytesB / DigitalStorageUnits.Byte
Me.SizeUnit = DigitalStorageUnits.Byte
Me.SizeUnitNameShort = "Bytes"
Me.SizeUnitNameLong = "Bytes"
End Select
End Sub
#End Region
#Region " Public Methods "
Public Overrides Function ToString() As String
Return Me.ToString(CultureInfo.InvariantCulture.NumberFormat)
End Function
Public Overloads Function ToString(provider As IFormatProvider) As String
Return If(Me.SizeUnit = DigitalStorageUnits.Byte,
String.Format(provider, "{0:0.##} {1}", System.Math.Floor(Me.SizeRounded * 100) / 100, Me.SizeUnitNameShort),
String.Format(provider, "{0:0.00} {1}", System.Math.Floor(Me.SizeRounded * 100) / 100, Me.SizeUnitNameShort))
End Function
Public Function ToStringFormatByteSize(flags As StrFormatByteSizeFlags) As String
Dim buffer As New StringBuilder(8, 16)
Dim result As Integer = NativeMethods.StrFormatByteSizeEx(CULng(Me.bytesB), flags, buffer, 16) ' HRESULT
If result <> 0 Then
Marshal.ThrowExceptionForHR(result)
End If
Return buffer.ToString()
End Function
#End Region
#Region " Private Methods "
<DebuggerStepThrough>
Private Shared Function Convert(size As Double,
fromUnit As DigitalStorageUnits,
toUnit As DigitalStorageUnits) As Double
Dim bytes As Double = If(fromUnit = DigitalStorageUnits.Byte, size, System.Convert.ToDouble(size * fromUnit))
If toUnit < fromUnit Then
Select Case toUnit
Case DigitalStorageUnits.Byte
Return bytes
Case DigitalStorageUnits.Kilobyte
Return bytes / DigitalStorageUnits.Kilobyte
Case DigitalStorageUnits.Megabyte
Return bytes / DigitalStorageUnits.Megabyte
Case DigitalStorageUnits.Gigabyte
Return bytes / DigitalStorageUnits.Gigabyte
Case DigitalStorageUnits.Terabyte
Return bytes / DigitalStorageUnits.Terabyte
Case DigitalStorageUnits.Petabyte
Return bytes / DigitalStorageUnits.Petabyte
Case Else
Throw New InvalidEnumArgumentException(argumentName:="toUnit", invalidValue:=CInt(toUnit),
enumClass:=GetType(DigitalStorageUnits))
End Select
ElseIf toUnit > fromUnit Then
Select Case toUnit
Case DigitalStorageUnits.Byte
Return bytes
Case DigitalStorageUnits.Kilobyte
Return bytes * DigitalStorageUnits.Kilobyte / (DigitalStorageUnits.Kilobyte ^ 2)
Case DigitalStorageUnits.Megabyte
Return bytes * DigitalStorageUnits.Megabyte / (DigitalStorageUnits.Megabyte ^ 2)
Case DigitalStorageUnits.Gigabyte
Return bytes * DigitalStorageUnits.Gigabyte / (DigitalStorageUnits.Gigabyte ^ 2)
Case DigitalStorageUnits.Terabyte
Return bytes * DigitalStorageUnits.Terabyte / (DigitalStorageUnits.Terabyte ^ 2)
Case DigitalStorageUnits.Petabyte
Return bytes * DigitalStorageUnits.Petabyte / (DigitalStorageUnits.Petabyte ^ 2)
Case Else
Throw New InvalidEnumArgumentException(argumentName:="toUnit", invalidValue:=CInt(toUnit),
enumClass:=GetType(DigitalStorageUnits))
End Select
Else ' If toUnit = fromUnit
Return bytes
End If
End Function
#End Region
End Class
End Namespace
#End Region
#Region " FileSizeConverter "
Namespace DevCase.Runtime.TypeConverters
Public Class FileSizeConverter : Inherits TypeConverter
#Region " Private Fields "
Private ReadOnly sizeUnitsDictionary
As Dictionary(Of String(), ULong
)
#End Region
#Region " Constructors "
Public Sub New()
Me.
sizeUnitsDictionary = New Dictionary(Of String(), ULong
)(StringComparison.
OrdinalIgnoreCase) From
{ {{"b", "byte", "bytes"}, 1UL},
{{"KB", "kilo", "kilos", "kilobyte", "kilobytes", "kilo byte", "kilo bytes"}, 1024UL},
{{"MB", "mega", "megas", "megabyte", "megabytes", "mega byte", "mega bytes"}, CULng(1024UL ^ 2UL)},
{{"GB", "giga", "gigas", "gigabyte", "gigabytes", "giga byte", "giga bytes"}, CULng(1024UL ^ 3UL)},
{{"TB", "tera", "teras", "terabyte", "terabytes", "tera byte", "tera bytes"}, CULng(1024UL ^ 4UL)},
{{"PB", "peta", "petas", "petabyte", "petabytes", "peta byte", "peta bytes"}, CULng(1024UL ^ 5UL)},
{{"EB", "exa", "exas", "exabyte", "exabytes", "exa byte", "exa bytes"}, CULng(1024UL ^ 6UL)}
}
End Sub
#End Region
#Region " Public Methods "
Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
Return sourceType Is GetType(String) OrElse
MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destinationType As Type) As Boolean
Return destinationType Is GetType(ULong) OrElse
destinationType Is GetType(String) OrElse
MyBase.CanConvertTo(context, destinationType)
End Function
<DebuggerStepThrough>
Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
If TypeOf value Is String Then
Dim strValue As String = DirectCast(value, String)
If strValue.Contains("("c) Then
strValue = strValue.Substring(0, strValue.IndexOf("("c))
End If
Dim bytes As ULong
If ULong.TryParse(strValue, bytes) Then
Return bytes
End If
bytes = Me.ParseFileSizeString(strValue)
If bytes <> 0UL Then
If context.PropertyDescriptor IsNot Nothing AndAlso context.PropertyDescriptor.PropertyType Is GetType(FileSize) Then
Return New FileSize(bytes, DigitalStorageUnits.Byte)
Else
Return DirectCast(bytes, Object)
End If
End If
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
<DebuggerStepThrough>
Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
If destinationType Is GetType(String) Then
Dim bytes As ULong = If((TypeOf value Is FileSize), CULng(DirectCast(value, FileSize).Size(DigitalStorageUnits.Byte)), DirectCast(value, ULong))
Dim formattedSize As String = Me.FormatByteFileSize(bytes)
Return If(bytes < 1024, String.Format("{0}", formattedSize), String.Format("{0} ({1:N0} bytes)", formattedSize, bytes))
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
#End Region
#Region " Private Methods "
<DebuggerStepThrough>
Private Function ParseFileSizeString(sizeString As String) As ULong
Dim trimmedSizeString As String = sizeString.Trim()
If trimmedSizeString.Contains("("c) Then
trimmedSizeString = trimmedSizeString.Substring(0, trimmedSizeString.IndexOf("("c))
End If
For Each kvp As KeyValuePair(Of String(), ULong) In sizeUnitsDictionary
For Each sizeUnitName As String In kvp.Key
If trimmedSizeString.EndsWith(sizeUnitName, StringComparison.OrdinalIgnoreCase) Then
Dim numString As String = trimmedSizeString.Substring(0, trimmedSizeString.Length - sizeUnitName.Length).Trim()
Dim num As Double
Dim numberFormat As IFormatProvider
#If NETCOREAPP Then
numberFormat = Thread.CurrentThread.CurrentUICulture.NumberFormat
#Else
numberFormat = My.Application.UICulture.NumberFormat
#End If
If Double.TryParse(numString, NumberStyles.Number, numberFormat, num) Then
Try
Return CULng((num * kvp.Value))
Catch ex As OverflowException
Return ULong.MaxValue
Catch ex As Exception
Throw
End Try
End If
End If
Next sizeUnitName
Next kvp
Throw New Exception("Cannot parse the specified file size value.")
End Function
<DebuggerStepThrough>
Private Function FormatByteFileSize(bytes As ULong) As String
Dim unitIndex As Integer = 0
Dim fileSize As Double = bytes
While (fileSize >= 1024) AndAlso (unitIndex < sizeUnitsDictionary.Count - 1)
fileSize /= 1024
unitIndex += 1
End While
Return If(unitIndex = 0,
String.Format("{0} bytes", fileSize),
String.Format("{0:F2} {1}", fileSize, sizeUnitsDictionary.ElementAt(unitIndex).Key(0)).Replace(",00", ""))
End Function
#End Region
End Class
End Namespace
#End Region
#Region " PropertyGridExtensions "
Namespace DevCase.Extensions.PropertyGridExtensions
<HideModuleName>
Public Module PropertyGridExtensions
#Region " Public Extension Methods "
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Function IsInitialized(propGrid As PropertyGrid) As Boolean
Return PropertyGridExtensions.GetInternalLabelWidth(propGrid) <> -1
End Function
<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Always)>
Public Sub SetSplitterPosition(propGrid As PropertyGrid, xPos As Integer)
If Not PropertyGridExtensions.IsInitialized(propGrid) Then
Throw New InvalidOperationException("The PropertyGrid must be initialized in order to set the splitter control position.")
End If
Dim internalGridView As Control = PropertyGridExtensions.GetPropertyGridInternal(propGrid)
Dim moveSplitterToMethod As MethodInfo = internalGridView.GetType().GetMethod("MoveSplitterTo", BindingFlags.NonPublic Or BindingFlags.Instance)
moveSplitterToMethod.Invoke(internalGridView, {xPos})
End Sub
#End Region
#Region " Private Methods "
Private Function GetPropertyGridInternal(propGrid As PropertyGrid) As Control
' Class: System.Windows.Forms.PropertyGridInternal.PropertyGridView
Dim getPropertyGridViewMethod As MethodInfo = GetType(PropertyGrid).GetMethod("GetPropertyGridView", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim gridView As Control = DirectCast(getPropertyGridViewMethod.Invoke(propGrid, Array.Empty(Of Object)()), Control)
Return gridView
' We can also retrieve the internal PropertyGridView this other way:
' ------------------------------------------------------------------
' Dim gridViewFieldInfo As FieldInfo = propGrid.GetType().GetField("gridView", BindingFlags.Instance Or BindingFlags.NonPublic)
' Dim gridView As Control = DirectCast(gridViewFieldInfo.GetValue(propGrid), Control)
' Return gridView
End Function
Private Function GetInternalLabelWidth(propGrid As PropertyGrid) As Integer
Dim internalGridView As Control = PropertyGridExtensions.GetPropertyGridInternal(propGrid)
Dim propInfo As PropertyInfo = internalGridView.GetType().GetProperty("InternalLabelWidth", BindingFlags.NonPublic Or BindingFlags.Instance)
Return CInt(propInfo.GetValue(internalGridView))
End Function
#End Region
End Module
End Namespace
#End Region
'@
$assembly = Add-Type -TypeDefinition $vbCode `
-CodeDomProvider (New-Object Microsoft.VisualBasic.VBCodeProvider) `
-PassThru `
-ReferencedAssemblies "Microsoft.VisualBasic.dll", `
"System.Windows.Forms.dll", `
"System.Security.dll", `
"System.Collections.dll", `
"System.Drawing.dll", `
"System.Globalization.dll", `
"System.Reflection.dll", `
"System.Linq.dll", `
"System.IO.dll", `
"System.Xml.dll", `
"System.Xml.Serialization.dll", `
"System.dll" `
| where { $_.IsPublic }
function Get-FormattedSize {
param (
[int64]$sizeInBytes
)
$buffer = New-Object System.Text.StringBuilder 24
$resultPtr = [DevCase.Win32.NativeMethods.ShlwApi]::StrFormatByteSize64A($sizeInBytes, $buffer, $buffer.Capacity)
if ($resultPtr -eq [IntPtr]::Zero) {
return "Error formatting the file size."
} else {
return $buffer.ToString()
}
}
function WriteDummyFile {
param (
[string]$FilePath,
[uint64]$FileSize,
[uint64]$BufferSize = 4MB,
[bool]$randomData = $false
)
$formattedFileSize = Get-FormattedSize -sizeInBytes $FileSize
$fs
= [System.
IO.
File]::Create
($FilePath
)
if ($BufferSize -ge $FileSize) {
$BufferSize = [math]::Floor($FileSize / 2)
}
[int64]$bytesWritten = 0
$buffer = New-Object byte[] $BufferSize
$rand = New-Object Random
Write-Host "Creating dummy file at '$FilePath' with a size of $formattedFileSize..."
while ($bytesWritten -lt $FileSize) {
$bytesToWrite = [math]::Min($BufferSize, $FileSize - $bytesWritten)
if ($randomData) {
$rand.NextBytes($buffer)
} else {
[Array]::Clear($buffer, 0, $bytesToWrite)
}
$fs.Write($buffer, 0, $bytesToWrite)
$bytesWritten += $bytesToWrite
$formattedBytesWritten = Get-FormattedSize -sizeInBytes $bytesWritten
$progressPercent = [math]::Min(($bytesWritten / $FileSize) * 100, 100)
Write-Progress -PercentComplete $progressPercent -Status "$FilePath" -Activity "Writing dummy file..." -CurrentOperation "$formattedBytesWritten written."
}
Write-Progress -PercentComplete 100 -Status "$FilePath" -Activity "Dummy file has been written." -CurrentOperation "$formattedBytesWritten written."
$fs.Close()
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host "Dummy file successfully created at '$FilePath' with a size of $formattedFileSize."
}
$form = New-Object CreateDummyFileOptionsForm
$dlgResult = $form.ShowDialog()
if ($dlgResult -eq [System.Windows.Forms.DialogResult]::OK) {
$fileOptions = $form.DummyFileOptions
WriteDummyFile -FilePath $fileOptions.FilePath -FileSize $fileOptions.FileSizeBytes -BufferSize $fileOptions.BufferSizeBytes -randomData $fileOptions.UseRandomData
} else {
Write-Host "Operation canceled. No file selected."
}
Write-Host "Press any key to exit..."
$key = [System.Console]::ReadKey($true)
Exit(0)