Jump to content











Photo
- - - - -

ImDiskAPI.ChangeFlags Read-Only and Removable USB stick

read-only changeflags c#

  • Please log in to reply
1 reply to this topic

#1 TATATA2

TATATA2
  • Members
  • 1 posts
  •  
    Germany

Posted 11 January 2016 - 02:14 PM

Hello,

 

I would like to create a usb device that is removable and readonly via the API. However, whenever I try to call ChangeFlags with the readonly attribute, it fails with "Access denied". I saw, that Readonly is a flag used for creation. However I am not able to figure out how to get the values in C# to the expected value 3 (as combination of enum value 1 and 2 for Readonly and removabale). 

 

The device gets created with the call:

ImDiskAPI.CreateDevice(1024 * 1024 * _sizeMB, 0, 0, 0, 0, ImDiskFlags.Removable, null, true, driveLetter, ref deviceID, IntPtr.Zero);

afterwards it gets formatted and I try to change it to a readonly device with:

 ImDiskAPI.ChangeFlags(deviceID, ImDiskFlags.ReadOnly, ImDiskFlags.ReadOnly);

I would like to have something, where I can also set the ReadOnly flag, but how? Thank you for your help in advance!

 

Kind regards, TATATA2.



#2 Olof Lagerkvist

Olof Lagerkvist

    Gold Member

  • Developer
  • 1448 posts
  • Location:Borås, Sweden
  •  
    Sweden

Posted 11 January 2016 - 02:59 PM

Normally in C#, when you want to combine enum flags you use the "or operator", that is "|":

ImDiskFlags.ReadOnly | ImDiskFlags.Removable

But, you would likely not want to do that when you create the virtual disk. If you set it to read-only already when you create it, you would not be able to format it so you could never really use it at all. So I would say that you are right that you should use ChangeFlags method to set it read-only when you are done with formatting and whatever initialization you want to do. I just made a quick test application:

long _sizeMB = 20;
var driveLetter = "Q:";
uint deviceID = uint.MaxValue;

long sizeBytes = _sizeMB * 1024 * 1024;

ImDiskAPI.CreateDevice(sizeBytes, 0, 0, 0, 0, ImDiskFlags.Removable, null, true, driveLetter, ref deviceID, IntPtr.Zero);

var geometry = DiscUtils.Geometry.FromCapacity(sizeBytes);

using (var volume = new ImDiskDevice(deviceID, FileAccess.ReadWrite))
{
    DiscUtils.Ntfs.NtfsFileSystem.Format(volume.GetRawDiskStream(), "", geometry, 0, sizeBytes / 512);
}

ImDiskAPI.ChangeFlags(deviceID, ImDiskFlags.ReadOnly, ImDiskFlags.ReadOnly);

This seems to work as far as I can see. I get a read-only drive Q: formatted as NTFS. Do you get any errors anywhere when you try the same?







Also tagged with one or more of these keywords: read-only, changeflags, c#

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users