windows 格式化磁盘

您所在的位置:网站首页 decheck下载 windows 格式化磁盘

windows 格式化磁盘

2024-06-17 15:24:44| 来源: 网络整理| 查看: 265

大家好,又见面了,我是你们的朋友全栈君。

新买来的硬盘是未初始化的,以我的理解就是没有引导扇区的,通常是没有MBR,如下图磁盘1,右边有大小,但显示“未分配”,

左边显示“没有初始化”, 点鼠标右键就可以【初始化磁盘】。

windows 格式化磁盘_磁盘0没有初始化windows 格式化磁盘_磁盘0没有初始化

初始化时可以选择MBR和GPT, MBR方式顶多支持2T硬盘的。

windows 格式化磁盘_磁盘0没有初始化windows 格式化磁盘_磁盘0没有初始化

初始化后

windows 格式化磁盘_磁盘0没有初始化windows 格式化磁盘_磁盘0没有初始化

初始化后可以新建简单卷了,之前是不行的:

windows 格式化磁盘_磁盘0没有初始化windows 格式化磁盘_磁盘0没有初始化

CreateDisk(1, 3) 就是把磁盘1分为3个分区

windows 格式化磁盘_磁盘0没有初始化windows 格式化磁盘_磁盘0没有初始化

奇怪,如果只分1个区,就是自动以NTFS格式化掉, 而分3个区,还会提示是否格式化。

如果想回到刚买回来的空白状态怎么办呢? 用DestroyDisk()就可以了

代码:

CPP:CMDiskManager.cpp

#include "stdafx.h" #include "CMDiskManager.h" CMDiskManager::CMDiskManager(){} //获取磁盘大小,单位是MB int CMDiskManager::GetDiskSize(DWORD vDiskNo) { HANDLE hDevice; // handle to the drive to be examined BOOL bResult; // results flag DWORD junk; // discard results char diskPath[256]; //磁盘内部路径 //生成磁盘内部路径 sprintf_s(diskPath, "\\\\.\\PhysicalDrive%d", vDiskNo); hDevice = CreateFile(TEXT(diskPath), // drive 或者 用"\\\\.\\PhysicalDrive0" 代表第一块磁盘 GENERIC_READ, // no access to the drive FILE_SHARE_READ | // share mode FILE_SHARE_WRITE, NULL, // default security attributes OPEN_EXISTING, // disposition 0, // file attributes NULL); // do not copy file attributes if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive { return (FALSE); } GET_LENGTH_INFORMATION pdg; bResult = DeviceIoControl(hDevice, // device to be queried IOCTL_DISK_GET_LENGTH_INFO, // operation to perform NULL, 0, // no input buffer &pdg, sizeof(pdg), // output buffer &junk, // # bytes returned (LPOVERLAPPED)NULL); // synchronous I/O CloseHandle(hDevice); /* INT64 nUseSize = disk_len.Length.QuadPart; INT64 sizeGB = nUseSize / 1014 / 1024 / 1024; CString szSize; szSize.Format(L"C盘大小 %I64d GB", sizeGB); */ int MB = pdg.Length.QuadPart >> 20; //CString s; //s.Format("C盘大小 %f GB", MB/1024.0); //AfxMessageBox(s, 0, MB_OK); //float x = (float) MB ; return MB ; } /* 获取磁盘分区个数 vDiskNo:磁盘序号 */ int CMDiskManager::GetPartNum(DWORD vDiskNo) { char diskPath[256]; //磁盘内部路径 //生成磁盘内部路径 sprintf_s(diskPath, "\\\\.\\PhysicalDrive%d", vDiskNo); HANDLE hDevice; //硬盘句柄 handle to the drive to be examined BOOL result; //结果标志 results flag DWORD readed; // discard results hDevice = CreateFile( diskPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, //default security attributes OPEN_EXISTING, // disposition 0, // file attributes NULL ); if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive { fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError()); return int(-1); } DRIVE_LAYOUT_INFORMATION_EX* dl; DWORD tSize = 0x4000; // sizeof(DRIVE_LAYOUT_INFORMATION_EX) * 10; dl = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(tSize); if (NULL == dl) { (void)CloseHandle(hDevice); return (int)-2; } result = DeviceIoControl( hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, dl, tSize, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_GET_DRIVE_LAYOUT_EX Error: %ld\n", GetLastError()); (void)CloseHandle(hDevice); return int(-3); } CString tPartitionStyle = "RAW"; switch (dl->PartitionStyle){ case 0: tPartitionStyle = "MBR"; break; case 1: tPartitionStyle = "GPT"; break; } //printf("dl->PartitionCount = %d", dl->PartitionCount); TRACE("dl->PartitionCount = %d, tPartitionStyle:%s \n", dl->PartitionCount, tPartitionStyle.GetBuffer()); //printf("dl->PartitionCount = %d", dl->PartitionCount); int tRet = dl->PartitionCount/4; //TRACE("dl->PartitionCount tRet = %d", tRet); free(dl); (void)CloseHandle(hDevice); return tRet; } //读MBR信息 BOOL CMDiskManager::ReadMBR(int vDiskNo, LPVOID *pBuffer) { HANDLE hDevice; DWORD dwSize; DWORD dwOverRead; BOOL bRet = TRUE; char diskPath[256]; //磁盘内部路径 //生成磁盘内部路径 sprintf_s(diskPath, "\\\\.\\PhysicalDrive%d", vDiskNo); hDevice = CreateFile(TEXT(diskPath), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hDevice == INVALID_HANDLE_VALUE) { TRACE("Open \\\\.\\PhysicalDrive failed. Error=%u\n", GetLastError()); return FALSE; } if (!DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL)) { CloseHandle(hDevice); TRACE("FSCTL_LOCK_VOLUME \\\\.\\PhysicalDrive0 failed. Error=%u\n", GetLastError()); return FALSE; } DISK_GEOMETRY Geometry; if (!DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &Geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) { bRet = FALSE; TRACE("IOCTL_DISK_GET_DRIVE_GEOMETRY \\\\.\\PhysicalDrive0 failed. Error=%u\n", GetLastError()); goto _out; } *pBuffer = (LPVOID)GlobalAlloc(GPTR, Geometry.BytesPerSector); if (*pBuffer) { if (!ReadFile(hDevice, *pBuffer, Geometry.BytesPerSector, &dwOverRead, NULL)) { printf("ReadFile \\\\.\\PhysicalDrive %u bytes failed. Error=%u\n", Geometry.BytesPerSector, GetLastError()); bRet = FALSE; } } _out: DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL); CloseHandle(hDevice); return bRet; } /* 获取磁盘分区信息 vDiskNo:磁盘序号 */ DWORD CMDiskManager::GetLayoutInfo(DWORD vDiskNo) { char diskPath[256]; //磁盘内部路径 //生成磁盘内部路径 sprintf_s(diskPath, "\\\\.\\PhysicalDrive%d", vDiskNo); HANDLE hDevice; //硬盘句柄 handle to the drive to be examined BOOL result; //结果标志 results flag DWORD readed; // discard results hDevice = CreateFile( diskPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, //default security attributes OPEN_EXISTING, // disposition 0, // file attributes NULL ); if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive { fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError()); return DWORD(-1); } DRIVE_LAYOUT_INFORMATION_EX* dl; DWORD tSize = 0x4000; // sizeof(DRIVE_LAYOUT_INFORMATION_EX) * 10; dl = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(tSize); if (NULL == dl) { (void)CloseHandle(hDevice); return (WORD)-1; } result = DeviceIoControl( hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, dl, tSize, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_GET_DRIVE_LAYOUT_EX Error: %ld\n", GetLastError()); (void)CloseHandle(hDevice); return DWORD(-1); } CString tPartitionStyle = "RAW"; switch (dl->PartitionStyle){ case 0: tPartitionStyle = "MBR"; break; case 1: tPartitionStyle = "GPT"; break; } //printf("dl->PartitionCount = %d", dl->PartitionCount); TRACE("dl->PartitionCount = %d, tPartitionStyle:%s", dl->PartitionCount, tPartitionStyle.GetBuffer()); free(dl); (void)CloseHandle(hDevice); return 0; } /* 初始化磁盘,创建分区 vDiskNo:磁盘序号,千万要避开系统盘,系统盘一般是0 vPartNum:分区数,只要1个分区就可以了 */ DWORD CMDiskManager::CreateDisk(DWORD vDiskNo, WORD vPartNum) { printf("准备CreateDisk, vDiskNo=%d, vPartNum=%d \n", vDiskNo, vPartNum); //第0块磁盘是系统盘,不能格式化掉!!!但不排除某些情况下新插入的移动硬盘会是第0块磁盘 if (0 == vDiskNo){ printf("第0块磁盘是系统盘,不能格式化掉\n"); return 0; } HANDLE hDevice; //硬盘句柄 handle to the drive to be examined BOOL result; //结果标志 results flag DWORD readed; // discard results DWORD ret; WORD i; char diskPath[256]; //磁盘内部路径 DISK_GEOMETRY pdg; DWORD sectorSize; //扇区大小 DWORD signature; //签名 LARGE_INTEGER diskSize; //磁盘大小 LARGE_INTEGER partSize; //分区大小 BYTE actualPartNum; //实际上的分区数 DWORD layoutStructSize; // DRIVE_LAYOUT_INFORMATION_EX *dl; //磁盘分区信息 CREATE_DISK newDisk; //创建磁盘(初始化?) //生成磁盘内部路径 sprintf_s(diskPath, "\\\\.\\PhysicalDrive%d", vDiskNo); actualPartNum = 4; if (vPartNum > actualPartNum) { printf("vPartNum > 4\n"); return (WORD)-1; } hDevice = CreateFile( diskPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, //default security attributes OPEN_EXISTING, // disposition 0, // file attributes NULL ); if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive { fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError()); return DWORD(-1); } // Create primary partition MBR //创建主分区的MBR printf("创建主分区的MBR\n"); newDisk.PartitionStyle = PARTITION_STYLE_MBR; signature = (DWORD)time(0); // 原为time(NULL), get signature from current time newDisk.Mbr.Signature = signature; result = DeviceIoControl( hDevice, IOCTL_DISK_CREATE_DISK, &newDisk, sizeof(CREATE_DISK), NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_CREATE_DISK Error: %ld\n", GetLastError()); (void)CloseHandle(hDevice); return DWORD(-1); } //fresh the partition table //刷新分区表 printf("刷新分区表\n"); result = DeviceIoControl( hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_UPDATE_PROPERTIES Error: %ld\n", GetLastError()); (void)CloseHandle(hDevice); return DWORD(-1); } //Now create the partitions //现在创建分区 ret = GetDriveGeometry(vDiskNo, &pdg); if ((DWORD)-1 == ret) { return ret; } //扇区大小 sectorSize = pdg.BytesPerSector; diskSize.QuadPart = pdg.Cylinders.QuadPart * pdg.TracksPerCylinder * pdg.SectorsPerTrack * pdg.BytesPerSector; //calculate the disk size; partSize.QuadPart = diskSize.QuadPart / vPartNum; //分区结构大小 layoutStructSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + (actualPartNum - 1) * sizeof(PARTITION_INFORMATION_EX); dl = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(layoutStructSize); if (NULL == dl) { (void)CloseHandle(hDevice); return (WORD)-1; } dl->PartitionStyle = (DWORD)PARTITION_STYLE_MBR; dl->PartitionCount = actualPartNum; dl->Mbr.Signature = signature; //clear the unused partitions //清除未用的分区 printf("清除未用的分区\n"); for (i = 0; i < actualPartNum; i++){ dl->PartitionEntry[i].RewritePartition = 1; dl->PartitionEntry[i].Mbr.PartitionType = PARTITION_ENTRY_UNUSED; } //set the profile of the partitions for (i = 0; i < vPartNum; i++){ dl->PartitionEntry[i].PartitionStyle = PARTITION_STYLE_MBR; dl->PartitionEntry[i].StartingOffset.QuadPart = (partSize.QuadPart * i) + ((LONGLONG)(pdg.SectorsPerTrack) * (LONGLONG)(pdg.BytesPerSector)); //32256 dl->PartitionEntry[i].PartitionLength.QuadPart = partSize.QuadPart; dl->PartitionEntry[i].PartitionNumber = i + 1; dl->PartitionEntry[i].RewritePartition = TRUE; dl->PartitionEntry[i].Mbr.PartitionType = PARTITION_IFS; dl->PartitionEntry[i].Mbr.BootIndicator = FALSE; dl->PartitionEntry[i].Mbr.RecognizedPartition = TRUE; dl->PartitionEntry[i].Mbr.HiddenSectors = pdg.SectorsPerTrack + (DWORD)((partSize.QuadPart / sectorSize) * i); } //execute the layout result = DeviceIoControl( hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, dl, layoutStructSize, NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_SET_DRIVE_LAYOUT_EX Error: %ld\n", GetLastError()); free(dl); (void)CloseHandle(hDevice); return DWORD(-1); } //fresh the partition table printf("刷新分区表\n"); result = DeviceIoControl( hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_UPDATE_PROPERTIES Error: %ld\n", GetLastError()); free(dl); (void)CloseHandle(hDevice); return DWORD(-1); } free(dl); (void)CloseHandle(hDevice); printf("CreateDisk完成\n"); Sleep(3000); //wait the operations take effect return 0; } //获取磁盘几何信息 BOOL CMDiskManager::GetDriveGeometry(DWORD vDiskNo, DISK_GEOMETRY *pdg) { HANDLE hDevice; // handle to the drive to be examined BOOL bResult; // results flag DWORD junk; // discard results char diskPath[256]; //磁盘内部路径 sprintf_s(diskPath, "\\\\.\\PhysicalDrive%d", vDiskNo); hDevice = CreateFile(TEXT(diskPath), // drive 0, // no access to the drive FILE_SHARE_READ | // share mode FILE_SHARE_WRITE, NULL, // default security attributes OPEN_EXISTING, // disposition 0, // file attributes NULL); // do not copy file attributes if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive { return (FALSE); } bResult = DeviceIoControl(hDevice, // device to be queried IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform NULL, 0, // no input buffer pdg, sizeof(*pdg), // output buffer &junk, // # bytes returned (LPOVERLAPPED)NULL); // synchronous I/O CloseHandle(hDevice); return (bResult); } /****************************************************************************** * Function: delete the partition layout of the disk 删除磁盘分区信息(恢复出厂设置) * input: disk, disk name * output: N/A * return: Succeed, 0 * Fail, -1 ******************************************************************************/ DWORD CMDiskManager::DestroyDisk(DWORD vDiskNo) { if (0 == vDiskNo){ //系统盘是0号盘,为了安全,不能删除 return 0; } HANDLE hDevice; // handle to the drive to be examined BOOL result; // results flag DWORD readed; // discard results CHAR diskPath[256]; sprintf(diskPath, "\\\\.\\PhysicalDrive%d", vDiskNo); hDevice = CreateFile( diskPath, // drive to open GENERIC_READ | GENERIC_WRITE, // access to the drive FILE_SHARE_READ | FILE_SHARE_WRITE, //share mode NULL, // default security attributes OPEN_EXISTING, // disposition 0, // file attributes NULL // do not copy file attribute ); if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive { fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError()); return DWORD(-1); } result = DeviceIoControl( hDevice, // handle to device IOCTL_DISK_DELETE_DRIVE_LAYOUT, // dwIoControlCode NULL, // lpInBuffer 0, // nInBufferSize NULL, // lpOutBuffer 0, // nOutBufferSize &readed, // number of bytes returned NULL // OVERLAPPED structure ); if (!result) { //fprintf(stderr, "IOCTL_DISK_DELETE_DRIVE_LAYOUT Error: %ld\n", GetLastError()); (void)CloseHandle(hDevice); return DWORD(-1); } //fresh the partition table result = DeviceIoControl( hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &readed, NULL ); if (!result) { fprintf(stderr, "IOCTL_DISK_UPDATE_PROPERTIES Error: %ld\n", GetLastError()); (void)CloseHandle(hDevice); return DWORD(-1); } (void)CloseHandle(hDevice); return 0; } /****************************************************************************** * Function:快速格式化某个磁盘,文件系统NTFS, 如果在CreateDisk()创建磁盘后,磁盘的文件系统是RAW的话,才需要调用该函数 * input: disk, disk name * output: N/A * return: Succeed, 0 * Fail, 1 ******************************************************************************/ DWORD CMDiskManager::FormatVolume(CHAR letter) { DWORD ret; CHAR cmd[64]; sprintf(cmd, "format %c: /FS:NTFS /Q /Y", letter); ret = (DWORD)system(cmd); return ret; } //获取第dwNum个磁盘的信息 void CMDiskManager::GetDiskInfo(DWORD &dwNum, CString chDriveInfo[]) { DWORD DiskCount = 0; //利用GetLogicalDrives()函数可以获取系统中逻辑驱动器的数量,函数返回的是一个32位无符号整型数据。 DWORD DiskInfo = GetLogicalDrives(); //通过循环操作查看每一位数据是否为1,如果为1则磁盘为真,如果为0则磁盘不存在。 while (DiskInfo) { //通过位运算的逻辑与操作,判断是否为1 Sleep(10); if (DiskInfo & 1) { DiskCount++; } DiskInfo = DiskInfo >> 1;//通过位运算的右移操作保证每循环一次所检查的位置向右移动一位。*/ } if (dwNum < DiskCount) { return;//实际的磁盘数目大于dwNum } dwNum = DiskCount;//将磁盘分区数量保存 //-------------------------------------------------------------------// //通过GetLogicalDriveStrings()函数获取所有驱动器字符串信息长度 int DSLength = GetLogicalDriveStrings(0, NULL); CHAR* DStr = new CHAR[DSLength]; memset(DStr, 0, DSLength); //通过GetLogicalDriveStrings将字符串信息复制到堆区数组中,其中保存了所有驱动器的信息。 GetLogicalDriveStrings(DSLength, DStr); int DType; int si = 0; BOOL fResult; unsigned _int64 i64FreeBytesToCaller; unsigned _int64 i64TotalBytes; unsigned _int64 i64FreeBytes; //读取各驱动器信息,由于DStr内部数据格式是A:\NULLB:\NULLC:\NULL,所以DSLength/4可以获得具体大循环范围 for (int i = 0; i{C, D, E} (disk0 has 3 drives, C:, D: and E:) * input: vDiskNo, disk's physical number * output: letters, letters array * return: Succeed, the amount of letters * Fail, -1 ******************************************************************************/ CString CMDiskManager::GetPartitionLetterFromPhysicalDrive(DWORD vDiskNo) { DWORD mask; DWORD driveType; DWORD bmLetters; DWORD diskNumber; CHAR path[256]; CHAR letter; DWORD letterNum; WORD i; CHAR *p; CString tRet = ""; bmLetters = GetLogicalDrives(); if (0 == bmLetters) { return ""; } letterNum = 0; for (i = 0; i < sizeof(DWORD) * 8; i++) { mask = 0x1u


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭