📄 isocreater.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.ComponentModel;
namespace IsoFtp
{
public class ISOCreater
{
/// <summary>
/// 进度条,Max=100,Min=1
/// </summary>
public int Proess;
/// <summary>
/// 进度信息事件
/// </summary>
/// <param name="mini">最小值</param>
/// <param name="max">最大值</param>
/// <param name="value">当前值</param>
/// <param name="sender">控件</param>
public delegate void OnProgressEventHandler(object sender, long mini, long max, long value);
/// <summary>
/// 进度信息事件
/// </summary>
[Description("进度信息事件")]
public event OnProgressEventHandler OnProgress;
/// <summary>
/// 检查CDROM是否准备完毕
/// </summary>
/// <param name="driverName">设备号</param>
/// <param name="volName">卷标名</param>
/// <returns>状态</returns>
public bool CDROMReady(string driverName, ref string volName)
{
DriveInfo di = new DriveInfo(driverName);
try
{
volName = di.VolumeLabel;
return (di.DriveType == DriveType.CDRom);
}
catch
{
return false;
}
}
/// <summary>
/// CDROM准备
/// </summary>
/// <param name="driverName"></param>
/// <returns></returns>
public bool CDROMReady(string driverName)
{
string tmpstr = "";
return CDROMReady(driverName, ref tmpstr);
}
/// <summary>
/// 获取光盘内容的大小
/// </summary>
/// <param name="driverName">设备号</param>
/// <returns></returns>
public long GetCDROMSize(string driverName)
{
DriveInfo di = new DriveInfo(driverName);
try
{
return di.TotalSize;
}
catch
{
return -1;
}
}
public static bool WorkState;
/// <summary>
/// 转换格式
/// </summary>
/// <param name="driverName"></param>
/// <param name="isoFileName"></param>
public long Convert(string driverName, string isoFileName)
{
ISOCreater.WorkState = true;
long FileSize = 0;
//打开数据
UnmanagedDriverLoader loader = new UnmanagedDriverLoader(driverName);
FileStream hDriver = new FileStream(loader.Handle, FileAccess.Read);
FileStream hIso = new FileStream(isoFileName, FileMode.Create, FileAccess.Write, FileShare.Write);
//10兆一个块
int bufferSize = 102400;
byte[] buffer = new byte[bufferSize];
//计算块的分页大小
long icount = (long)(GetCDROMSize(driverName) / bufferSize);
FileSize = (long)GetCDROMSize(driverName);
if ((GetCDROMSize(driverName) % bufferSize) != 0)
icount++;
long length = bufferSize;
hDriver.Seek(0, SeekOrigin.Begin);
hIso.Seek(0, SeekOrigin.Begin);
long MaxSize = GetCDROMSize(driverName);
//遍历块写到文件
for (long i = 0; i < icount; i++)
{
if (!WorkState)
{
hIso.Close();
System.IO.File.Delete(isoFileName);
break;
}
hDriver.Read(buffer, 0, (int)length);
hIso.Write(buffer, 0, (int)length);
length = GetCDROMSize(driverName) - hDriver.Position;
//给出进度条百分比
Proess = int.Parse((hIso.Length * 100 / MaxSize).ToString());
if (OnProgress != null)
{
OnProgress(this, 0, GetCDROMSize(driverName), hIso.Length);
}
if (length > bufferSize)
length = bufferSize;
}
hDriver.Close();
hIso.Close();
return FileSize;
}
[DllImport("winmm.dll", EntryPoint = "mciSendString")]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
UInt16 uReturnLength,
IntPtr hwndCallback
);
public static string GetDefaultCDRom()
{
string MyCdRom = "";
string s = "";
StringBuilder volumeName = new StringBuilder(256);
int srNum = new int();
int comLen = new int();
string sysName = "";
int sysFlags = new int();
int result;
string[] logDrives = System.IO.Directory.GetLogicalDrives();
bool Defaulted = false;
for (int i = 0; i < logDrives.Length; i++)
{
if (CdRomeApi.GetDriveType(logDrives[i]) == 5)
{
if (!Defaulted)
{
MyCdRom = logDrives[i].ToString();
Defaulted = true;
}
s += "Your CD ROM is on drive : " + logDrives[i].ToString() + "\n";
result = CdRomeApi.GetVolumeInformation(logDrives[i].ToString(), volumeName, 256, srNum, comLen, sysFlags, sysName, 256);
if (result == 0)
s += "CD ROM中没光盘";//不必用try,catch吧
else
{
s += "CD ROM 中有光盘,名称: " + volumeName;
}
}
}
return MyCdRom.Replace(@"\","");
}
}
class CdRomeApi
{
[DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
public static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
[DllImport("kernel32.dll", EntryPoint = "GetVolumeInformationA")]
public static extern int GetVolumeInformation(string lpRootPathName, StringBuilder lpVolumeNameBuffer, int nVolumeNameSize, int lpVolumeSerialNumber, int lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize);
[DllImport("kernel32.dll", EntryPoint = "GetDriveTypeA")]
public static extern int GetDriveType(string nDrive);
}
/// <summary>
/// 使用 SafeFileHandle 类和非托管 CreateFile 函数打开 Win32 设备
/// </summary>
class UnmanagedDriverLoader
{
public const short FILE_ATTRIBUTE_NORMAL = 0x80;
public const short INVALID_HANDLE_VALUE = -1;
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint CREATE_NEW = 1;
public const uint CREATE_ALWAYS = 2;
public const uint OPEN_EXISTING = 3;
[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
private SafeFileHandle handleValue = null;
public UnmanagedDriverLoader(string driverName)
{
Load(driverName);
}
public void Load(string driverName)
{
if (driverName == null && driverName.Length == 0)
{
throw new ArgumentNullException("driverName");
}
handleValue = CreateFile("\\\\.\\" + driverName, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (handleValue.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
public SafeFileHandle Handle
{
get
{
if (!handleValue.IsInvalid)
{
return handleValue;
}
else
{
return null;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -