I'm quite lost with this task. Basically I have to check if a pendrive has readonly property.
- If it's protected I have to unprotect it (we use DISKPART.exe), update some files, then protect it again.
- If it's not protected, I just have to update files then protect it again.
This is my code:
private void protectpendrive(string disk)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine("select volume " + disk);
p.StandardInput.WriteLine("attributes disk set readonly");
debugListView.Items.Add(DateTime.Now + "Pendrive is now read-only.");
p.StandardInput.WriteLine("exit");
}
To unprotect:
private void unprotectpendrive(string disk)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine("select volume " + disk);
p.StandardInput.WriteLine("attributes disk clear readonly");
debugListView.Items.Add(DateTime.Now + "Pendrive is now not protected.");
p.StandardInput.WriteLine("exit");
}
This is my control for the current status, but it doesn't work either:
private bool checkreadonly(string disk)
{
DirectoryInfo pendrive = new DirectoryInfo(disk);
if (pendrive.Attributes.HasFlag(FileAttributes.ReadOnly))
{
debugListView.Items.Add(DateTime.Now + " Pendrive is read-only.");
return true;
}
debugListView.Items.Add(DateTime.Now + " Pendrive is writable.");
return false;
}
The issues I have are:
- Diskpart requires administrator priviledges, I tried with StartInfo.Verb="runas" but it seems it's not taken into consideration
- If I launch my app with administrator rights, the diskpart window opens but it doesn't close at the end. Overall the status doesn't change.
What am I doing wrong?
The following shows how to both set and clear the readonly attribute for a removable USB disk.
Create a new
Windows Forms App (.NET Framework)
project (name: ProcessDiskPartTest)Add an Application Manifest to your project
Note: This is used to prompt the user to execute the program as Administrator.
In app.manifest, replace
with
Option 1 - Diskpart (script)
The following shows how to create a diskpart script and then use System.Diagnostics.Process to execute the script.
Add the following using directives:
using System.IO;
using System.Diagnostics;
The following code will use Process to execute a
diskpart
script.Usage (Clear Readonly):
Usage (Set Readonly):
Option 2 - Diskpart (StandardInput)
The following shows how to call diskpart by using System.Diagnostics.Process and sending commands via StandardInput
Add the following using directives:
using System.IO;
using System.Diagnostics;
Usage (Clear Readonly):
Usage (Set Readonly):
Option 3 - PowerShell:
The following shows how to use PowerShell to set or clear the readonly attribute for a removable USB drive.
Download/install the appropriate PowerShell NuGet package:
For .NET Framework, choose one of the following:
For .NET (version >= 6), choose one of the following:
See Choosing the right PowerShell NuGet package for your .NET project for more information and Differences between Windows PowerShell 5.1 and PowerShell 7.x
Add the following using directives:
using System.Management.Automation.Runspaces
using System.Management.Automation;
using System.Diagnostics;
Usage (Clear Readonly):
Usage (Set Readonly):
Resources: