Ciao a tutti.
Esiste un metodo in C# per capire se un drive è protetto da scrittura?
Ciao a tutti.
Esiste un metodo in C# per capire se un drive è protetto da scrittura?
Il metodo più rapido è provare a scriverci direttamente ed essere pronti a gestire eventuali errori, dato che per sapere se "in teoria" ci puoi scrivere dovresti verificare se il percorso sta su un device di sola scrittura (ad esempio un CD), se per caso ci sono ACL che impediscono all'utente corrente di scrivere nel percorso in questione, se lo spazio su disco è sufficiente, ... Senza contare che tra il momento del controllo e l'uso effettivo queste proprietà possono cambiare, per cui devi essere comunque pronto a gestire gli errori.
Amaro C++, il gusto pieno dell'undefined behavior.
Ho già provato a fare come dici tu provando a scrivere nella root dei vari drive che provo ad andare a controllare però ho trovato un caso particolare e a dir la verità forse neanche tanto particolare:
Ho un portatile con Windows Seven e provavo ad andare a scrivere in C: però non lo permetteva, invece se vado manualmente a creare una cartella o un file giustamente me lo permette.
Probabilmente c'è dietro qualche magia di UAC che non conosco bene: con ogni probabilità tu sei amministratore, ma le applicazioni di default viaggiano con privilegi ridotti, non consentendoti (giustamente) di scrivere nella root del drive; Windows Explorer probabilmente, prima di creare le cartelle in zone protette per codice in esecuzione a privilegi ridotti si auto-eleva (credo che faccia parte dei warning UAC soppressi dall'impostazione di UAC a livello intermedio).
Il suggerimento che ti ho dato resta comunque valido.
Amaro C++, il gusto pieno dell'undefined behavior.
Originariamente inviato da thedestroyer90
Ho già provato a fare come dici tu provando a scrivere nella root dei vari drive che provo ad andare a controllare però ho trovato un caso particolare e a dir la verità forse neanche tanto particolare:
Ho un portatile con Windows Seven e provavo ad andare a scrivere in C: però non lo permetteva, invece se vado manualmente a creare una cartella o un file giustamente me lo permette.
Mostra il codice...
codice:foreach (DriveInfo drI in DriveInfo.GetDrives()) { try { File.Create(drI.RootDirectory + "\\file"); File.Delete(drI.RootDirectory + "\\file"); } catch (Exception exc) { } }
Trovata la soluzione:
codice:public bool VerifyDriveProtected(string driveName) { StringBuilder volname = new StringBuilder(261); StringBuilder fsname = new StringBuilder(261); uint sernum, maxlen; FileSystemFeature flags; if (!GetVolumeInformation(driveName, volname, volname.Capacity, out sernum, out maxlen, out flags, fsname, fsname.Capacity)) Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); string volnamestr = volname.ToString(); string fsnamestr = fsname.ToString(); bool responso = (flags & FileSystemFeature.ReadOnlyVolume) > 0; return responso;// se è true il drive è protetto } public enum FileSystemFeature : uint { /// <summary> /// The file system supports case-sensitive file names. /// </summary> CaseSensitiveSearch = 1, /// <summary> /// The file system preserves the case of file names when it places a name on disk. /// </summary> CasePreservedNames = 2, /// <summary> /// The file system supports Unicode in file names as they appear on disk. /// </summary> UnicodeOnDisk = 4, /// <summary> /// The file system preserves and enforces access control lists (ACL). /// </summary> PersistentACLS = 8, /// <summary> /// The file system supports file-based compression. /// </summary> FileCompression = 0x10, /// <summary> /// The file system supports disk quotas. /// </summary> VolumeQuotas = 0x20, /// <summary> /// The file system supports sparse files. /// </summary> SupportsSparseFiles = 0x40, /// <summary> /// The file system supports re-parse points. /// </summary> SupportsReparsePoints = 0x80, /// <summary> /// The specified volume is a compressed volume, for example, a DoubleSpace volume. /// </summary> VolumeIsCompressed = 0x8000, /// <summary> /// The file system supports object identifiers. /// </summary> SupportsObjectIDs = 0x10000, /// <summary> /// The file system supports the Encrypted File System (EFS). /// </summary> SupportsEncryption = 0x20000, /// <summary> /// The file system supports named streams. /// </summary> NamedStreams = 0x40000, /// <summary> /// The specified volume is read-only. /// </summary> ReadOnlyVolume = 0x80000, /// <summary> /// The volume supports a single sequential write. /// </summary> SequentialWriteOnce = 0x100000, /// <summary> /// The volume supports transactions. /// </summary> SupportsTransactions = 0x200000, }