简体   繁体   English

如何以编程方式格式化未分配的空间?

[英]How to programmatically format unallocated space?

I'm looking for a code snippet that would programmatically format unallocated space on a given drive. 我正在寻找一个可以以编程方式格式化给定驱动器上未分配空间的代码段。

I'm ideally looking for .net code (VB.NET C#) or C++. 我理想地是在寻找.net代码(VB.NET C#)或C ++。

Many thanks 非常感谢

The best option seems to be to use WMI. 最好的选择似乎是使用WMI。 In particular, you'll want to look into the Format method of the Win32_Volume class. 特别是,您需要查看Win32_Volume类的Format方法。

You could instead use the SHFormatDrive function of the Win32 API. 您可以改用Win32 API的SHFormatDrive函数。 This may be the simpler option (especially in C++), though I'm not sure how the functionality compares with the WMI method. 这可能是更简单的选项(尤其是在C ++中),尽管我不确定功能与WMI方法的比较。 Edit: As M. Jahedbozorgan points out, this seems to open the Explorer shell dialog to format the drive. 编辑:正如M. Jahedbozorgan指出的那样,这似乎打开了Explorer外壳程序对话框来格式化驱动器。

A third option is to run the command-line format.exe from code (and then read from the stdout stream), but this clearly isn't a very nice solution. 第三种选择是从代码运行命令行format.exe (然后从stdout流中读取),但这显然不是一个很好的解决方案。

Other suggestions are given in this thread on MSDN forums. 在MSDN论坛上的该线程中给出了其他建议。

Create a file that fills up the entire drive, then write random data to it, then flush it, then close it. 创建一个充满整个驱动器的文件,然后向其中写入随机数据,然后刷新并关闭它。

The first step is probably the hardest to do reliably across multiple configurations (eg, NTFS compressed volumes or per-user quotas). 跨多个配置(例如NTFS压缩卷或每个用户的配额)可靠地执行第一步可能是最困难的。

Or use "cipher /w" from the command line. 或从命令行使用“ cipher / w”。

It looks like Windows only provides a method to format an entire volume, not just the unallocated space. Windows似乎仅提供一种格式化整个卷的方法,而不仅仅是未分配的空间。

So, if I understand your question right, you could create a partition in the remaining space of the drive, then format that new partition. 因此,如果我正确理解您的问题,则可以在驱动器的剩余空间中创建一个分区,然后格式化该新分区。

You will want to use PInvoke and WMI's Win32_Volume. 您将要使用PInvoke和WMI的Win32_Volume。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM