PDA

View Full Version : چطوری بفهمیم فلش به سیستم وصل شده است یا خیر



kasraaa
شنبه 07 شهریور 1394, 10:03 صبح
سلام چطوری بفهمیم فلش ب سیستم وصل شده است یا خیر

ahmadreza.s
شنبه 07 شهریور 1394, 10:19 صبح
سلام با اين روش ميتونيد ليست يو اس بي هايي كه به سيستم وصل هست رو ببينيد !

ابتدا رفرنس System.Managment رو به پروژتون اضافه ميكنيد !

بعد از اين كد استفاده كنيد :

namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.

class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();

foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}

Console.Read();
}

static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();

foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}

collection.Dispose();
return devices;
}
}

class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}


منبع (http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices)

kasraaa
شنبه 07 شهریور 1394, 10:34 صبح
واسه ویندوز فرمی چجوریه؟

kasraaa
یک شنبه 08 شهریور 1394, 08:05 صبح
یعنی کسی بلد نیست؟

محمد رضا فاتحی
یک شنبه 08 شهریور 1394, 08:13 صبح
نگاه کن دوست عزیز...بستگی داره چطور تو محیط ویندوز فرم بخواین هندل کنی
اگه بنا رو بزاریم که کد ارسالی دوستنمون درسته... که حتما هست!
توابع و کلاس اصلی کد ارسالی رو تو محیط ویندوز فرم پیاده سازی کن و تابع GetUSBDevices لیست و اطلاعات یو اس بی های متصل به سیستم رو به صورت لیست برات استخراج می کنه و می تونی از لیست استفاده کنی...

kasraaa
یک شنبه 08 شهریور 1394, 09:25 صبح
میشی با کد واسم تبدیلش کنی؟

محمد رضا فاتحی
یک شنبه 08 شهریور 1394, 09:48 صبح
بفرمایید

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();


ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();


foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}


collection.Dispose();
return devices;
}


private void Form1_Load(object sender, EventArgs e)
{
var device =GetUSBDevices();
if (device.Count != 0)
{
foreach (var usbDevice in device)
{
listBox1.Items.Add(string.Format("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description));


}
}
else
{
MessageBox.Show("دستگاه یو اس بی یافت نگردید");
}
}
}


class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}

kasraaa
یک شنبه 08 شهریور 1394, 10:04 صبح
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))


این چیه؟
ارور گرفته

محمد رضا فاتحی
یک شنبه 08 شهریور 1394, 10:21 صبح
فضای نام System.Management احتمالا پیدا نشده...
باید به لیست رفرنس هات اضافش کنی

kasraaa
یک شنبه 08 شهریور 1394, 11:03 صبح
درست نشد و نمیشه

محمد رضا فاتحی
یک شنبه 08 شهریور 1394, 11:08 صبح
اینو دانلود کن و به پروژت اضافه کن
http://s3.picofile.com/file/8209218500/System_Management.dll.html

kasraaa
دوشنبه 09 شهریور 1394, 07:12 صبح
برنامه خوده دی ال ال رو نمیشناسه

ahmadreza.s
دوشنبه 09 شهریور 1394, 08:08 صبح
سلام
واسه اضافه کردن رفرنس System.Managment ابتدا به پنجره ی Solution Explorer میرید (اگه اینو نداشتید از گزینه ی view بالا ، Solution Explorer رو بزنید) بعد روی قسمت Reference کلیک راست میکنید ، بعد گزینه ی Add Reference رو میزنید !

134778

بعد تو پنجره ی باز شده ، طبق عکس زیر ، گزینه ی System.Managment رو تیکشو میزنی !


134779

و بعد در قسمت using ها ، میای ازش استفاده میکنی ! اینجوری :
using System.Managment;