Recipe 7.11. Making a Disk or Volume Read-Only


Problem

You want to make a disk or volume read-only so that users or programs can't write to it.

Solution

Using downloadable software

The Joeware (http://www.joeware.net) tool, writeprot, allows you to make a disk or volume read-only as long as no files are locked on that volume. The following command lists the read/write state of all disks and volumes on a system:

> writeprot /mview

The following command attempts to make the D drive read-only:

> writeprot /vol d: /ro

The following command makes the D drive read-write:

> writeprot /vol d: /rw

The following command attempts to make all volumes on the basic disk represented by the D drive read-only:

> writeprot /vol d: /ro

Using VBScript
' This code makes a volume or disk read-only (if possible) ' ------ SCRIPT CONFIGURATION ------ strDrive = "<Drive>"  ' e.g. e: ' This assumes writeprot is in your PATH, if not, fully qualify ' the path to the command (e.g. c:\bin\writeprot.exe) strCommand = "writeprot /vol " & strDrive & " /ro" ' ------ END CONFIGURATION --------- set objWshShell = WScript.CreateObject("WScript.Shell") intRC = objWshShell.Run(strCommand, 0, TRUE) if intRC <> 0 then    WScript.Echo "Error returned from running the command: " & intRC    WScript.Echo "Command attempted: " & strCommand else    WScript.Echo "Command executed successfully." end if

Discussion

Volumes on basic disks and dynamic disks are treated differently when it comes to enabling write protection. You can make individual volumes on a dynamic disk read-only, but for basic disks, you have to write-protect the entire disk, including all volumes. If you attempt to write-protect a single volume on a basic disk with multiple volumes using writeprot, an error will be returned. You'd need to run the same command with the -unsafe option to write-protect all volumes on the basic disk.

You cannot write protect any volume that has files locked for read or write access. Since files are always opened for reading on the system volume, you will not be able to write protect that volume.

It was reported by some of the technical reviewers of this book that the third-party drivers do NOT properly support write protection.


See Also

Recipe 8.13 for making a file or folder read-only and Recipe 8.19 for finding open files



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net