I'm making a Windows Phone 8.1 Silverlight, and when I try to read a file, and upon reading, the following error occurs:
An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: Access to the path 'C:\Data\Users\Public\Pictures\Camera Roll\WP_20150421_001.jpg' is denied.
{
string filepath = FilePathTextBox.Text;
string cryptPath = filepath;
File.SetAttributes(cryptPath, System.IO.FileAttributes.Normal);
FileStream stream = File.OpenRead(cryptPath);
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
byte[] ProtectedFileBytes = ProtectedData.Protect(fileBytes, null);
using (Stream file = File.OpenWrite(@"C:\Data\Users\Public\Documents"))
{
file.Write(ProtectedFileBytes, 0, ProtectedFileBytes.Length);
}
MessageBox.Show("File Sucessfully Encrypted");
}
All of the relevant libraries are checked in the manifest, I'm running Visual Studio as administrator, I've tried different files, all to no avail.
I added a check to verify if the file exists, and that flagged as false, but that is likely due to the permissions problem.
Any suggestions as to solving this problem?
Edit:
I now have this code, which is throwing a System.ArgumentException:
private async void EncryptButton_Click(object sender, RoutedEventArgs e)
{
string filepath = FilePathTextBox.Text;
string cryptPath = filepath;
await FileOps();
FileStream stream = File.OpenRead(cryptPath);
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
byte[] ProtectedFileBytes = ProtectedData.Protect(fileBytes, null);
using (Stream file = File.OpenWrite(@"C:\Data\Users\Public\Documents"))
{
file.Write(ProtectedFileBytes, 0, ProtectedFileBytes.Length);
}
MessageBox.Show("File Sucessfully Encrypted");
}
private async Task FileOps()
{
string filepath = FilePathTextBox.Text;
string cryptPath = filepath;
StorageFolder storageFolder = KnownFolders.CameraRoll;
StorageFile file = await storageFolder.CreateFileAsync(cryptPath, CreationCollisionOption.ReplaceExisting);
}