This is a code sample to give you an idea how to incorporate a Format function to your solution in C#.
The Format functionality helps you select the right file system format for a new partition, or change the file system format of an existing volume. NTFS and FAT file system formats are supported.
Due to internal restrictions, hdmengine cannot work on multiple processes at the same time, so it locks the volume during the process to restrict access from another process. For example, if an additional process is initialized (for example, resize a partition) when the hdmengine is busy with a different process (for example, creating a partition), the engine will not be able to lock the additional process (resize a partition) and will execute it in the Bluescreen mode.
Before you read the code sample:
- add hdmengine_hdmclientinteroplib.dll library to your project.
There are three basic steps in the tutorial: hdmengine initialization, executing the Format function, and closing the hdmengine and API process.
Define the Paragon.DiskMgmt namespace to bring hdmengine interfaces into global scope, and System – to bring standard interfaces.
namespace ExampleHdm
{
using Paragon.DiskMgmt;
using System;
Declare the main class of the application.
public class PartitionFormatApp
{
Entry point to application. Launch the main function with the administrator privileges. Be aware that GPT disk has hidden partitions, that are not shown in Disk Management UI. There are three parameters that you introduce – disk number and partition number.
public static int Main(String[] cliArgs)
{
if(cliArgs.Length != 2)
{
Console.WriteLine("Usage: partition_format.exe <disk number> <partition number>");
return -1;
}
int diskNumber = Convert.ToInt32(cliArgs[0]);
int partitionNumber = Convert.ToInt32(cliArgs[1]);
Specify the Hdmengine logs directory.
String logsDirectory = "C:\\Windows\\Temp\\hdmengine_example";
Specify the Hdmengine bluescreen (kernel) mode logs directory.
String bsLogsDirectory = "C:\\Windows\\Temp\\hdmengine_example";
String customCopyright = "My custom copyright";
Initialize progress callbacks interface.
ProgressOutput progress = new ProgressOutput();
To be able to use API methods – create Hdmengine in-process interface.
IHdmengineApi hdmInProcess = new HdmengineInProcess(progress, logsDirectory);
Initialize the hdmengine.
String version;
uint status = hdmInProcess.Init(HdmclientInitFlags.EnableRunMultiple, out version);
if (status == (uint)DiskMgmtOperationStatus.SERR_SUCCESS)
Console.WriteLine("Hdmclient initialized, version = {0}", version);
else
{
Console.WriteLine("Hdmclient initialized, version = {0}, status = {1}", version, status);
return -2;
}
Create the layout.ini file to change the parameters of the hdmengine. This file also contains the path to the bluescreen logs. Remember to place the bluescreen logs folder on a different volume to the one you are performing operations on.
status = hdmInProcess.ChangeLayoutSettings(true, bsLogsDirectory, customCopyright);
if (status != (uint)DiskMgmtOperationStatus.SERR_SUCCESS)
{
Console.WriteLine("Hdmengine can'not change layout {0}", status);
return -2;
}
To perform partition operations, you need to get the list of disks and partitions available in the system first.
DiskInfoExtracted[] disks;
status = hdmInProcess.GetDisksInfo(out disks);
if (status != (uint)DiskMgmtOperationStatus.SERR_SUCCESS)
{
Console.WriteLine("Failed to get disks info {0}", status);
return -2;
}
if (disks == null || disks.Length == 0)
{
status = (uint)DiskMgmtOperationStatus.SERR_DISK_NOT_FOUND;
Console.WriteLine("Disk not found {0}", status);
return -2;
}
This is the main section to enter the code for the Format functionality (see the detailed breakdown for the code below). You can add more than one functionality in queue of operations.
status = PartitionFormatFs(hdmInProcess, disks, diskNumber, partitionNumber, PartitionFsType.Ntfs, "New Volume", 0);
if (status != (uint)DiskMgmtOperationStatus.SERR_SUCCESS)
return -2;
Apply any pending operations or the queue of operations. This process also installs the bluescreen reboot, if required. Bluescreen jobs that need a restart, can be checked as hdmInProcess.IsNeedReboot().
status = hdmInProcess.Apply();
if(status != (uint)DiskMgmtOperationStatus.SERR_SUCCESS)
{
Console.WriteLine("Hdmengine can'not apply operation {0}", status);
}
Close Hdmengine, free all resources. Reboot system, if there were operations, that required reboot. If instead of the hdmInProcess.IsNeedReboot value, a False is defined, the user needs to reboot the application explicitly.
hdmInProcess.Close(hdmInProcess.IsNeedReboot(), false);
return 0;
}
Format functionality setup – select the partition or volume from the list of the available disks and partitions. ClusterSize in sectors 1, 2, 4, 8, …
static uint PartitionFormatFs(IHdmengineApi hdmApi, DiskInfoExtracted[] disks, int diskNumber, int partitionNumber, PartitionFsType fsType, String label, uint clusterSizeSectors)
{
uint err = (uint)DiskMgmtOperationStatus.SERR_SUCCESS;
bool foundDisk = false;
bool foundPartition = false;
PartitionInfoExtracted partInfo = new PartitionInfoExtracted();
DiskInfoExtracted diskInfo = new DiskInfoExtracted();
if (diskNumber >= disks.Length)
{
err = (uint)DiskMgmtOperationStatus.SERR_DISK_NOT_FOUND;
Console.WriteLine($"Disk number is out of range (maximum number is {disks.Length}, diskNumber = {diskNumber}), {err}");
return err;
}
else
{
diskInfo = disks[diskNumber];
foundDisk = true;
if (diskInfo.partitions == null || (partitionNumber >= diskInfo.partitions.Length))
{
err = (uint)DiskMgmtOperationStatus.SERR_PART_NOT_FOUND;
Console.WriteLine($"Partition number is out of range (maximum number is {diskInfo.partitions.Length}, partitionNumber is {partitionNumber}), {err}");
return err;
}
else
{
partInfo = diskInfo.partitions[partitionNumber];
foundPartition = true;
}
}
if ((partInfo.ActFlags & PartitionActFlags.CanFormat) == 0)
{
err = (uint)DiskMgmtOperationStatus.SERR_INCOMPATIBLEOP;
Console.WriteLine($"Format file system is not enabled for selected partition ... Failed, {err}\n");
return err;
}
Verify that the Format function is available and can be performed on the selected partition.
if ((partInfo.ActFlags & PartitionActFlags.CanFormat) == 0)
{
err = (uint)DiskMgmtOperationStatus.SERR_INCOMPATIBLEOP;
Console.WriteLine($"Format is not enabled for selected partition ... Failed, {err}\n");
return err;
}
if (fsType == PartitionFsType.Fat16)
{
if ((partInfo.FsTypeMaskFormat & PartitionFsTypeMask.Fat16) == 0)
{
err = (uint)DiskMgmtOperationStatus.SERR_INCOMPATIBLEOP;
Console.WriteLine($"Format to Fat16 is not enabled for selected partition ... Failed, {err}\n");
return err;
}
}
else
if (fsType == PartitionFsType.Fat32)
{
if ((partInfo.FsTypeMaskFormat & PartitionFsTypeMask.Fat32) == 0)
{
err = (uint)DiskMgmtOperationStatus.SERR_INCOMPATIBLEOP;
Console.WriteLine($"Format to Fat32 is not enabled for selected partition ... Failed, {err}\n");
return err;
}
}
else
if (fsType == PartitionFsType.Ntfs)
{
if ((partInfo.FsTypeMaskFormat & PartitionFsTypeMask.Ntfs) == 0)
{
err = (uint)DiskMgmtOperationStatus.SERR_INCOMPATIBLEOP;
Console.WriteLine($"Format to Ntfs is not enabled for selected partition ... Failed, {err}\n");
return err;
}
}
else
{
err = (uint)DiskMgmtOperationStatus.SERR_INCOMPATIBLEOP;
Console.WriteLine($"Format to selected fs is not enabled for selected partition ... Failed, {err}\n");
return err;
}
Call to engine method. Add the format filesystem operation to queue. The actual job will be done on engine.Apply().
err = hdmApi.PartitionFormat(partInfo.DiskNumber,
partInfo.PartitionNumber,
fsType,
label,
clusterSizeSectors);
if (err == (uint)DiskMgmtOperationStatus.SERR_SUCCESS)
Console.WriteLine($"Format selected partition file system queue ... Success\n");
else
{
Console.WriteLine($"Format selected partition file system queue ... Failed, {err}\n");
return err;
}
return err;
}
}
}