I want to write a file to removable disk of my raspberry pi.
I knew I needed to mount the removable disk, so I did that from the command line.
Here is my code:
public string ExcuteCommand(string command)
{
ProcessStartInfo _processStartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c "{command}"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process? _process = Process.Start(_processStartInfo);
string _result = "";
if (_process is { })
{
_result = _process.StandardOutput.ReadToEnd();
_process.WaitForExit();
_process.Close();
}
return _result;
}
Then I execute commands as below:
sudo mount -t vfat /dev/sda1 ./usb/
sudo chown -R admin:admin ./usb/
After that, I can use
File.ReadAllText(path)
to read files in removable disk successfully.
However, when I tried write file to it by:
File.WriteAllText(Path, content);
.Net reports an error:
System.UnauthorizedAccessExeption:Access to the path '/home/admin/usb/123.txt' is denied.
System.IO.IOException:Premission denied.
I find it very strange that I clearly used chown to obtain write permission, but why does it still prompt that permission is not obtained?
Moreover, if I do not write the file through the .Net program, but use the command line in putty to directly enter the commands one by one above and use sudo touch 123.txt
to create the file, the file can be created normally.
What is going on? How can I solve the problem?