我有一個關於USB disk.i的項目,在我的代碼中彈出了usb磁盤。但是我想知道如何重新加載它?任何人都可以給我一些建議。 更好的一個例子!thx如何在C#中重新加載USB磁盤?
2
A
回答
1
我已經解決了這樣這個問題設備的工作,流程是我的代碼:
win32.cs
using System;
使用System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
命名空間SystemDevices { [StructLayout(LayoutKind.Sequential)] 公共結構SP_BROADCAST_HANDLE { 公衆詮釋dbch_size; public int dbch_devicetype; public int dbch_reserved; public IntPtr dbch_handle; public IntPtr dbch_hdevnotify; public Guid dbch_eventguid; public long dbch_nameoffset; public byte dbch_data; public byte dbch_data1; }
[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
}
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
public int cbSize;
public Guid classGuid;
public int devInst;
public IntPtr reserved;
};
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINSTALL_PARAMS
{
public int cbSize;
public int Flags;
public int FlagsEx;
public IntPtr hwndParent;
public IntPtr InstallMsgHandler;
public IntPtr InstallMsgHandlerContext;
public IntPtr FileQueue;
public IntPtr ClassInstallReserved;
public int Reserved;
[MarshalAs(UnmanagedType.LPTStr)]
public string DriverPath;
};
[StructLayout(LayoutKind.Sequential)]
public class SP_PROPCHANGE_PARAMS
{
public SP_CLASSINSTALL_HEADER ClassInstallHeader = new SP_CLASSINSTALL_HEADER();
public int StateChange;
public int Scope;
public int HwProfile;
};
[StructLayout(LayoutKind.Sequential)]
public class SP_CLASSINSTALL_HEADER
{
public int cbSize;
public int InstallFunction;
};
public class Win32
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, DEV_BROADCAST_DEVICEINTERFACE NotificationFilter, UInt32 Flags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern UInt32 UnregisterDeviceNotification(IntPtr hHandle);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPStr)]String Enumerator, IntPtr hwndParent, Int32 Flags);
[DllImport("setupapi.dll")]
public static extern IntPtr SetupDiGetClassDevsEx(ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPStr)]String Enumerator,
IntPtr hwndParent, Int32 Flags, IntPtr DeviceInfoSet,
[MarshalAs(UnmanagedType.LPStr)]String MachineName,
IntPtr Reserved);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr lpInfoSet);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern Boolean SetupDiEnumDeviceInfo(IntPtr lpInfoSet, Int32 dwIndex, SP_DEVINFO_DATA devInfoData);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern Boolean SetupDiGetDeviceRegistryProperty(IntPtr lpInfoSet, SP_DEVINFO_DATA DeviceInfoData, UInt32 Property,
UInt32 PropertyRegDataType, StringBuilder PropertyBuffer, UInt32 PropertyBufferSize, IntPtr RequiredSize);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Boolean SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SP_PROPCHANGE_PARAMS ClassInstallParams, int ClassInstallParamsSize);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiClassNameFromGuid(ref Guid ClassGuid, StringBuilder className, Int32 ClassNameSize, ref Int32 RequiredSize);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiGetClassDescription(ref Guid ClassGuid, StringBuilder classDescription, Int32 ClassDescriptionSize, ref Int32 RequiredSize);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, StringBuilder DeviceInstanceId, Int32 DeviceInstanceIdSize, ref Int32 RequiredSize);
public const int DIGCF_ALLCLASSES = (0x00000004);
public const int DIGCF_PRESENT = (0x00000002);
public const int INVALID_HANDLE_VALUE = -1;
public const int SPDRP_DEVICEDESC = (0x00000000);
public const int MAX_DEV_LEN = 200;
public const int DEVICE_NOTIFY_WINDOW_HANDLE = (0x00000000);
public const int DEVICE_NOTIFY_SERVICE_HANDLE = (0x00000001);
public const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = (0x00000004);
public const int DBT_DEVTYP_DEVICEINTERFACE = (0x00000005);
public const int DBT_DEVNODES_CHANGED = (0x0007);
public const int WM_DEVICECHANGE = (0x0219);
public const int DIF_PROPERTYCHANGE = (0x00000012);
public const int DICS_FLAG_GLOBAL = (0x00000001);
public const int DICS_FLAG_CONFIGSPECIFIC = (0x00000002);
public const int DICS_ENABLE = (0x00000001);
public const int DICS_DISABLE = (0x00000002);
public static bool StateChange(bool Enable, int SelectedItem, IntPtr DevInfo)
{
bool result = false;
SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); ;
devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
if (true == SetupDiEnumDeviceInfo(DevInfo, SelectedItem, devInfoData))
{
SP_PROPCHANGE_PARAMS pcp = new SP_PROPCHANGE_PARAMS(); ;
pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER));
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.Scope = DICS_FLAG_GLOBAL;
pcp.StateChange = (Enable ? DICS_ENABLE : DICS_DISABLE);
if (true == SetupDiSetClassInstallParams(DevInfo, devInfoData, pcp, Marshal.SizeOf(pcp)))
{
if (true == SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, DevInfo, devInfoData))
{
result = true;
}
}
}
return result;
}
public static String GetClassNameFromGuid(Guid guid)
{
String result = String.Empty;
StringBuilder className = new StringBuilder();
Int32 iRequiredSize = 0;
Int32 iSize = 0;
bool b = SetupDiClassNameFromGuid(ref guid, className, iSize, ref iRequiredSize);
className = new StringBuilder(iRequiredSize);
iSize = iRequiredSize;
b = SetupDiClassNameFromGuid(ref guid, className, iSize, ref iRequiredSize);
if (true == b)
{
result = className.ToString();
}
return result;
}
public static String GetClassDescriptionFromGuid(Guid guid)
{
String result = String.Empty;
StringBuilder classDesc = new StringBuilder(0);
Int32 iRequiredSize = 0;
Int32 iSize = 0;
bool b = SetupDiGetClassDescription(ref guid, classDesc, iSize, ref iRequiredSize);
classDesc = new StringBuilder(iRequiredSize);
iSize = iRequiredSize;
b = SetupDiGetClassDescription(ref guid, classDesc, iSize, ref iRequiredSize);
if (true == b)
{
result = classDesc.ToString();
}
return result;
}
public static String GetDeviceInstanceId(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData)
{
String result = String.Empty;
StringBuilder id = new StringBuilder(0);
Int32 iRequiredSize = 0;
Int32 iSize = 0;
bool b = SetupDiGetDeviceInstanceId(DeviceInfoSet, DeviceInfoData, id, iSize, ref iRequiredSize);
id = new StringBuilder(iRequiredSize);
iSize = iRequiredSize;
b = SetupDiGetDeviceInstanceId(DeviceInfoSet, DeviceInfoData, id, iSize, ref iRequiredSize);
if (true == b)
{
result = id.ToString();
}
return result;
}
}
}
在win32.cs有一些功能來控制硬件。
函數StateChange(bool Enable,int SelectedItem,IntPtr DevInfo)用於控制硬件啓動或停止。 有三個Parameters.when啓用是真實的,這個功能使得硬件衛生組織ID是的SelectedItem開始,如有虛假stop.i獲得此代碼ID:
Guid classGuid = Guid.Empty;
IntPtr hDevInfo = Win32.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, Win32.DIGCF_ALLCLASSES | Win32.DIGCF_PRESENT);
if (hDevInfo.ToInt32() == Win32.INVALID_HANDLE_VALUE)
{
Console.WriteLine("read hardware information error");
}
else
{
int i = 0;
StringBuilder deviceName = new StringBuilder();
deviceName.Capacity = Win32.MAX_DEV_LEN;
do
{
SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
devInfoData.classGuid = Guid.Empty;
devInfoData.devInst = 0;
devInfoData.reserved = IntPtr.Zero;
bool result = Win32.SetupDiEnumDeviceInfo(hDevInfo, i, devInfoData);
if (false == result)
{
break;
}
Console.WriteLine("Device: {0}", i);
Console.WriteLine("\tGuid={0}", devInfoData.classGuid);
Console.WriteLine("\tName={0}", Win32.GetClassNameFromGuid(devInfoData.classGuid));
Console.WriteLine("\tDescription={0}", Win32.GetClassDescriptionFromGuid(devInfoData.classGuid));}
0
有一個可以從微軟獲得的工具(devcon),可以用來實現設備管理器的功能。
以下命令用於刪除一個特定的設備(假設你的設備名稱是金士頓)
> devcon remove *Kingston*
這將導致要禁用該設備。所以它不再可用。 命令重新掃描啓用設備。
> devcon rescan
您可以通過Process.Start()方法執行此命令。 這並不與已標記爲「安全刪除」
2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
private void btnenable_Click(object sender, EventArgs e)
{
const string keyName = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\USBSTOR";
// int tLong = (int)Registry.GetValue(keyName, "Start",0);
Registry.SetValue(keyName, "Start", "00000003");
MessageBox.Show("USB MassStorage Enabled");
}
private void btndisable_Click(object sender, EventArgs e)
{
const string keyName = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\USBSTOR";
// int tLong = (int)Registry.GetValue(keyName, "Start",0);
Registry.SetValue(keyName, "Start", "00000004");
MessageBox.Show("USB MassStorage Disabled");
}
相關問題
- 1. 在Windows上安裝/卸載USB磁盤
- 2. 如何從磁盤加載字體?
- 3. 如何讓Sublime Text 2在磁盤上更改時重新加載文件?
- 4. 用Perl腳本重新加載磁盤分區列表
- 5. 從磁盤加載程序
- 6. 如何在嵌入式linux中讀取usb磁盤卷標?
- 7. 獲取Qt中的USB磁盤列表
- 8. 在C#中保存/加載巨型布爾數組到磁盤?
- 9. 如何重新加載C#中的toolsrtipcontrol#
- 10. 如何在C#中清空/刷新Windows READ磁盤緩存?
- 11. 如何在磁盤
- 12. 在驅動程序級別的USB磁盤加密
- 13. php從usb磁盤更改文件名
- 14. C#編寫的CookieContainer到磁盤和加載早在適用
- 15. 從C#中的磁盤加載文件 - System.IO.IOException
- 16. 如何在iOS上從磁盤加載plist文件作爲NSDictionary?
- 17. 如何重新加載UITableViewCell Objective C?
- 18. 在c中重新加載linux服務
- 19. 如何在iPhone中重新加載UIPickerView?
- 20. 如何在JBoss中重新加載類
- 21. 如何在Java中重新加載JDialog?
- 22. 從Bundle或磁盤加載更好嗎?
- 23. HttpConnection,UITableView和RunLoops並從磁盤加載
- 24. 快速從磁盤加載UITableViewCell圖像
- 25. SVN加載的磁盤有多大
- 26. 如何在Linux下獲取C/C++中的SCSI磁盤列表?
- 27. Python生成器可以輕鬆保存並從磁盤重新加載嗎?
- 28. 如何從磁盤
- 29. 檢測塊設備是本地磁盤還是可移動USB磁盤
- 30. 在C#中設置USB鍵盤狀態
但這usbdisk已標記爲「安全移除「 – hcemp 2010-12-08 01:01:44