On Windows Server, by default, external USB disks don't always get mounted. I'd like my program (written in C#) to be able to detect external USB disks, identify them, and then mount them.
When it's finished, it should do whatever the programmatic equivalent of "Safely Remove Hardware" in order to flush and unmount the disk.
Any pointers?
You are searching for the functions SetVolumeMountPointA and DeleteVolumeMountPointW.
In order to find Volumes you could use FindFirstVolumeW and FindNextVolumeW.
Related
I want to add a backup functionality to my app in the sense that the app will allow users to create a self-contained environment (data + executable + autorun.inf file) on an external device (CD, CD-ROM or USB key) so that users can retrieve (in read only mode) their stored environment at a given point in time.
I've read here about making an auto-bootable USB (which I suspect will be the same for a CD or DVD).
edit I meant "auto-executing", not "auto-bootable"!
I know about IMAPI or other approaches that are now several years old. Is there any alternative/standard approach to create media this way? I'd like a generic way to create either CD/DVD or USB devices (non-bootable).
You need to make sure to not confuse Booting and Windows Autorun.
auto-bootable USB
refers to an external USB drive from which you can boot your computer.
I doubt that's what you are looking for, given the linked Stackexchange question. The information you already found is actually what you are looking for, and the advice given there seems pretty accurate.
What you need to do is:
Create the media with your data. In case of USB sticks this is as easy as copying the data to the stick. Optical media (CD/DVD) need to be burned, and that can only be done using the Windows API, which has already be discussed here.
If you want to use the Windows autorun feature (which I would ignore*), you'll need to create a file called Autorun.ini in the root directory of your media. You already have found the required content of that file here.
* Why I would skip the autorun feature.
First of all, there's no real alternative to the Windows autorun feature.
It was controversial, at best, in earlier versions of Windows. Since USB sticks went mainstream, autorun became nothing more than an automatic virus installation feature. Soon after everyone started disabling it. Because of this recent versions of Windows seem to have dropped it or disable it by default.
An exception to this would be the Icon=diskicon.ico option, which allows you to change the icon of the drive. This might be still working, even if autorun is disabled.
I have to make a program which monitor usb ports and when an usb device is plugged (joypad, flash drives, mouse, ecc...) I get a unique identifier (a deviceid or something else would be good).
At first I tried with C# using the system.management classes and querying the cim_logicaldevice class each second to get the new device plugged.
Some device returned more rows with DeviceID, but this isn't a problem. The problem is that the memory occupied by the program (in task manager) grows up constantly.
This is the source code:
http://pastebin.com/dQv3cMQC
Is there a way to avoid the growing of the memory usege?
I have to do this program in C++ or C# and it has to be the most efficient possible (because it has to be opened forever).
I would recommend looking at the USBView sample in the WDK. If you are unfamiliar with this, simply run it - this tool walks the entire USB tree on the system and prints out information and descriptor listings for each device.
In your case, I'd start at the RefreshTree() function in this sample, you can then follow the code to see how it enumerates the host controllers, hubs and finally devices. For each device that you find you can look at the bInterfaceClass in the interface descriptors to find out what types of interfaces it is advertising.
The easiest way to get the source to this sample is to install the 7.1.0 WDK which is currently available here: http://www.microsoft.com/en-us/download/details.aspx?id=11800
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C#: Create a virtual drive in Computer
My aim is to create a fake virtual drive to share on the network. The purpose of this fake drive is simply to receive files transferred to it through the network, and then discard them all immediately (not interested in ever having them touch any solid disk drive).
The reason I need this is to simulate a recording server receiving video feed from hundreds of cameras, without actually having to store the data. Storing the data would require a heavy RAID-0 setup which I don't have plans on getting. I simply need to measure the system load on Machine A when sending the video over the network to Machine B. The fake virtual drive would be installed on Machine B and then shared on the network to be accessible by Machine A.
So, in short I need to:
Create a drive letter, which by Windows will seem like any other drive.
Manage the I/O on this "drive", perhaps by overriding a method or two, to simply skip the part where I'd normally be saving the data.
The important part is that Machine A (the machine sending the files) is unaffected by Machine B not being a real shared storage device.
Thanks in advance for any feedback.
I have seen this done with a Windows file system driver written in C. It does what you are looking for, i.e. behaves just like a physical hard drive but development is not for the faint of heart.
The driver is capable of doing whatever you want in response to operating system messages. For example, a "write" could persist to a database, increment a counter in memory, or do nothing at all.
Here is a third party SDK I found: http://www.eldos.com/cbfs/index.php?referer1=google&referer2=adwords_cbfs_gen&gclid=CO7Tnbm086wCFRBphwodjQ_XOQ
I want to block a USB based on volume using C#. Like I want to block USB stick if capacity is greater than 8GB.
Look there is a method to block USB on PC using registry. but this will make USB undetectable so I can not get volume information.
I want to do that If my client program is running on some machine and I put restriction that USB capacity >=8GB should be restricted, so my C# code should safely remove the USB and should now show any balloon, I mean silently remove it.
If you are disabling the USBSTOR key, then you prevent "ACCESS" to it, that includes gathering information from it.
I suggest you look here: WM_DEVICECHANGE
Using this you can catch when the USB is entered and then get the drive letter and use the DEV_BROADCAST_VOLUME to gather information on it, then you can disable it using your registry method.
I am working on a project where I need to access some specific addresses of a USB drive (e.g. sector 0), and change some bytes. I have done some parts with C# already that includes user interface, detection of USB drives etc. Can anybody help me providing some links so that I can access specific addresses of USB drives with .NET?
The Framework doesn't support this. If you attempt to create a FileStream on a device it will throw an exception. You will have to use the Windows API methods directly (p/invoke CreateFile, DeviceIoControl, etc). Make sure you read the section on Physical Disks and Volumes here:
http://msdn.microsoft.com/en-us/library/aa363858.aspx