i'm getting a "System.UnauthorizedAccessException: Access is denied. " message when im trying to save a list on the Windows Phone.
Scenario:
When clicked on the FavoriteButton, the system will check if the item is already a favorite.
The next step is to call the Load method. This will return a list with favorites.
When the item is favorite I will remove it from the list, otherwise i will add it.
Final step is to save the new list with my Save method.
When executing this code i get the following exception:
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED
I've looked around and saw that it could be the manifest capabilities. So i have checked them all just to be sure. After trying again I still get the exception!
What must i do? code below:
private async void favoriteClicked(object sender, EventArgs e)
{
if (_progressIndicator.IsVisible == false)
{
try
{
Boolean isFavorite = Settings.GetValueOrDefault<Boolean>(currentArtist.id, false);
ArtistSaving favorite = new ArtistSaving(currentArtist.id, currentArtist.name, currentArtist.shortBio);
artistList = await Task.Run(() => Utils.Load<ArtistSaving>());
if (isFavorite == true)
{
artistList.Remove(favorite);
displayDeletePopup();
Settings.AddOrUpdateValue(currentArtist.id, false);
await Task.Run(() => Utils.Save<ArtistSaving>(artistList));
}
else
{
artistList.Add(favorite);
displayXamlPopup();
Settings.AddOrUpdateValue(currentArtist.id, true);
await Task.Run(() => Utils.Save<ArtistSaving>(artistList));
}
}
catch (ArgumentException ex)
{
}
}
}
Favorite button click
public static async Task<ObservableCollection<T>> Load<T>()
{
// Create a new folder name DataFolder.
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(JSONFILENAME,
CreationCollisionOption.OpenIfExists);
var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<T>));
var myStream = await file.OpenStreamForReadAsync();
ObservableCollection<T> myObj = (ObservableCollection<T>)jsonSerializer.ReadObject(myStream);
if (myObj == null)
{
return new ObservableCollection<T>();
}
return myObj;
}
This is the Load method
public static async void Save<T>(ObservableCollection<T> obj)
{
var serializer = new DataContractJsonSerializer(typeof(ObservableCollection<T>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
JSONFILENAME, CreationCollisionOption.ReplaceExisting))
{
serializer.WriteObject(stream, obj);
}
}
and this was the Save method
EDIT -- SOLUTION
public static async Task<ObservableCollection<T>> Load<T>()
{
ObservableCollection<T> myObj = null;
// Create a new folder name DataFolder.
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(JSONFILENAME,
CreationCollisionOption.OpenIfExists);
var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<T>));
using (var myStream = await file.OpenStreamForReadAsync())
{
myObj = (ObservableCollection<T>)jsonSerializer.ReadObject(myStream);
if (myObj == null)
{
return new ObservableCollection<T>();
}
return myObj;
}
}
Most likely the file is still locked from when you read from it. Modify your Load method like this to release the file lock:
using (var myStream = await file.OpenStreamForReadAsync())
{
ObservableCollection<T> myObj = (ObservableCollection<T>)jsonSerializer.ReadObject(myStream);
}
Related
I have an Xamarin.Forms-based app which runs on Android and iOS. Right now, I am implementing the feature of selecting images from the camera roll and uploading it to our server. Therefore, I am writing platform-specific code for iOS, which is where the error occurs.
I am calling the UIImagePickerController from a platform-specific renderer for iOS. It opens normally. But when tapping on an image in the UIImagePickerController nothing happens, except Visual Studio showing a message in the debug console:
"Warning: Attempt to present Xamarin_Forms_Platform_iOS_ModalWrapper: 0x155a7ed00 on Xamarin_Forms_Platform_iOS_PlatformRenderer: 0x153ead6a0 whose view is not in the window hierarchy!"
I googled and found somebody writing a function called "GetVisibleViewController" which i adapted to my project (you can see it below). On the ViewController which that function returns, I call the PresentModalViewController() method. Unfortunately, it is not working either. It is not possible to select a photo.
private void ChoosePhoto()
{
_imagePicker = new UIImagePickerController()
{
SourceType = UIImagePickerControllerSourceType.PhotoLibrary,
MediaTypes = new string[] { UTType.Image }
};
_imagePicker.FinishedPickingMedia += delegate (object sender, UIImagePickerMediaPickedEventArgs e)
{
var fileName = eopAppLibrary.Tools.GetTimestampJpegFileName("ScanToEop_iOS");
var jpegImageData = e.OriginalImage.AsJPEG();
var jpegBytes = jpegImageData.ToArray();
Events.RaiseFilePreviewNeeded(this, jpegBytes, fileName);
};
_imagePicker.Canceled += delegate (object sender, EventArgs e)
{
_imagePicker.DismissModalViewController(true);
};
var viewController = GetVisibleViewController();
viewController.PresentModalViewController(_imagePicker, true);
}
UIViewController GetVisibleViewController(UIViewController controller = null)
{
controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;
if (controller.PresentedViewController == null)
{
return controller;
}
if (controller.PresentedViewController is UINavigationController)
{
return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
}
if (controller.PresentedViewController is UITabBarController)
{
return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
}
return GetVisibleViewController(controller.PresentedViewController);
}
We had a similar issue and here is what we came up with:
var topViewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
var controllerToPresentWith = topViewController.VisibleViewController();
controllerToPresentWith.PresentModalViewController(_imagePicker, true);
and then
...
public static UIViewController VisibleViewController(this UIViewController controller)
{
if (controller == null)
return null;
if (controller is UINavigationController navController)
{
return navController.VisibleViewController();
}
else if (controller is UITabBarController tabController)
{
tabController.SelectedViewController?.VisibleViewController();
}
else
{
var vc = controller.PresentedViewController?.VisibleViewController();
if (vc != null)
return vc;
}
return controller;
}
In the end, I implemented this by using James Montemagno's Media Plugin library (available over NuGet: https://www.nuget.org/packages/Xam.Plugin.Media) and Permissions Plugin (https://www.nuget.org/packages/Plugin.Permissions).
I wrote the following code for this:
private async Task ChoosePhoto()
{
var permission = await CheckCameraRollPermission();
if (permission == PermissionStatus.Granted)
{
await CrossMedia.Current.Initialize();
// Show image picker dialog
var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
{
ModalPresentationStyle = Plugin.Media.Abstractions.MediaPickerModalPresentationStyle.OverFullScreen
});
if (file != null)
{
// Image has been selected
using (var stream = file.GetStream())
{
using (var memoryStream = new System.IO.MemoryStream())
{
stream.CopyTo(memoryStream);
var fileBytes = memoryStream.ToArray();
// DO WHATEVER YOU WANT TO DO WITH THE SELECTED IMAGE AT THIS POINT
}
}
}
}
}
private async Task<PermissionStatus> CheckCameraRollPermission()
{
// Check permission for image library access
var permission = await PermissionsImplementation.Current.CheckPermissionStatusAsync(Permission.Photos);
if (permission != PermissionStatus.Granted)
{
// Permission has not been granted -> if permission has been requested before and the user did not grant it, show message and return the permission
var message = "";
switch (permission)
{
case PermissionStatus.Denied:
case PermissionStatus.Restricted:
message = "Unfortunately, you did not grant permission to access the camera roll. If you want to change this, you can do so in the system settings of your device.";
break;
default:
break;
}
if (!string.IsNullOrEmpty(message))
{
// Message available -> Display alert and return the permission
var alert = UIAlertController.Create("Permission not granted", message, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(alert, true, null);
return permission;
}
// In all other cases, request the permission
await PermissionsImplementation.Current.RequestPermissionsAsync(Permission.Photos);
// Check for permission one more time and return it
permission = await PermissionsImplementation.Current.CheckPermissionStatusAsync(Permission.Photos);
}
return permission;
}
I want to observe on a folder. Like i want a Event When the content is changed.
I found this
var options = new Windows.Storage.Search.QueryOptions
{
FolderDepth = Windows.Storage.Search.FolderDepth.Deep
};
var query = Folder.CreateFileQueryWithOptions(options);
query.ContentsChanged += QueryContentsChanged;
var files = await query.GetFilesAsync();
private void QueryContentsChanged(IStorageQueryResultBase sender, object args)
{
//Code here
}
But the problem with this is I can't find which file caused the event and i even can't know what caused the Event (Like Modify , Create , Delete or Rename of file) How to get these details?
I used this code
public async void MonitorFolder()
{
var options = new Windows.Storage.Search.QueryOptions
{
FolderDepth = Windows.Storage.Search.FolderDepth.Deep
};
var query = Folder1.CreateFileQueryWithOptions(options);
query.ContentsChanged += QueryContentsChanged;
var files = await query.GetFilesAsync();
await addtoOld(Folder1, Old);
}
private async void addtoOld(StorageFolder folder1, List<FDate> old)
{
var files = await folder1.GetFilesAsync();
foreach (var file in files)
{
BasicProperties basicProperties = await file.GetBasicPropertiesAsync();
FDate f = new FDate
{
Path = file.Path,
Id = file.FolderRelativeId,
Modified = basicProperties.DateModified,
Change = ChangeType.NoChange,
FileType = Type.File
};
old.Add(f);
}
var folders = await folder1.GetFoldersAsync();
foreach (var folder in folders)
{
BasicProperties basicProperties = await folder.GetBasicPropertiesAsync();
FDate f = new FDate
{
Path = folder.Path,
Id = folder.FolderRelativeId,
Modified = basicProperties.DateModified,
Change = ChangeType.NoChange,
FileType = Type.Folder
};
old.Add(f);
addtoOld(folder, old);
}
return;
}
private async void QueryContentsChanged(IStorageQueryResultBase sender, object args)
{
New.Clear();
List<FDate> changed = new List<FDate>();
await addtoOld(Folder1, New);
foreach(var f in New)
{
var f1 = getFile(f);
if (f1 != null)
{
if (f1.Modified < f.Modified)
{
f1.Change = ChangeType.Modified;
changed.Add(f1);
}
Old.Remove(f1);
}
else
{
f.Change = ChangeType.Created;
changed.Add(f);
}
}
foreach (var f in Old)
{
f.Change = ChangeType.Deleted;
changed.Add(f);
}
Old = New;
foreach (var f in changed)
{
if(f.FileType== Type.File)
{
if (f.Change == ChangeType.Modified)
{
//code here
}
if(f.Change == ChangeType.Created)
{
//Created code here
}
if(f.Change == ChangeType.Deleted)
{
//Deleted code here
}
}
else
{
if (f.Change == ChangeType.Created)
{
//Created code here
}
if(f.Change == ChangeType.Deleted)
{
//Deleted code here
}
}
}
private FDate getFile(FDate f)
{
foreach(var fi in Old)
{
if (f.Name == fi.Name)
return fi;
}
return null;
}
This code is not working properly I looks like it is because the addtoOld is async The code can't be substituted because it is recursive. and the function can't be made sync it has many await how do i solve this?
Note:OLD and New are Lists ChangeType and Type are enums
According to the following blog post, there is unfortunately no way to identify the reason of the event and there is also no information about affected items.
File system change notifications in WinRT: http://lunarfrog.com/blog/filesystem-change-notifications
So I guess you will have to go through all files and check their Properties property to determine when each file was last modified, created etc.: https://learn.microsoft.com/en-us/uwp/api/windows.storage.storagefile
In my app, I am using OneDrive to keep data in sync. I am successfully writing the file to OneDrive, but am having no luck replacing the local outdated data with the newer OneDrive data.
My current method, which completes without throwing an exception, does not return the same text data that the file on OneDrive contains.
Goal of the method is to compare the datemodified to the OneDrive file to the local file, and if OneDrive is newer, write the contents of the OndeDrive file to the local StorageFile, and then return it to be de-serialized.
private async Task<string> GetSavedDataFileAsync(string filename)
{
string filepath = _appFolder + #"\" + KOWGame + #"\" + filename;
StorageFile localread;
BasicProperties localprops = null;
string txt;
try
{
localread = await local.GetFileAsync(filepath);
localprops = await localread.GetBasicPropertiesAsync();
}
catch (FileNotFoundException)
{ localread = null; }
if (_userDrive != null)
{
if (_userDrive.IsAuthenticated)
{
try
{
Item item = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync();
if (item != null)
{
DateTimeOffset drivemodified = (DateTimeOffset)item.FileSystemInfo.LastModifiedDateTime;
if (localprops != null)
{
if (drivemodified > localprops.DateModified)
{
Stream stream = await localread.OpenStreamForWriteAsync();
using (stream)
{ await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync(); }
}
}
}
}
catch (OneDriveException e)
{
if (e.IsMatch(OneDriveErrorCode.ActivityLimitReached.ToString()))
{ string stop; }
}
}
}
if (localread == null) return string.Empty;
txt = await FileIO.ReadTextAsync(localread);
return txt;
}
I tried to reverse engineer another answer I found on Stack regarding writing a StorageFile to OneDrive, in that I needed to open the stream of the local file, but I doesn't appear to be working properly.
To get the content of a OneDrive item, we need use following method:
var contentStream = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().GetAsync();
While using
await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync();
you are getting the OneDrive Item not its content.
So you can change your code like following to write the content of a Onedrive item to a local file:
if (drivemodified > localprops.DateModified)
{
using (var stream = await localread.OpenStreamForWriteAsync())
{
using (var contentStream = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().GetAsync())
{
contentStream.CopyTo(stream);
}
}
}
I'm trying to download multiple files from an FTP server using WebClient.DownloadFileTaskAsync and repeatedly have the issue that several files end up being 0KB.
I've tried different suggested solutions but I just don't manage to get all files. What am I doing wrong?
class Program
{
static void Main()
{
Setup(); // This only sets up working folders etc
Task t = ProcessAsync();
t.ContinueWith(bl =>
{
if (bl.Status == TaskStatus.RanToCompletion)
Logger.Info("All done.");
else
Logger.Warn("Something went wrong.");
});
t.Wait();
}
private static void Setup() {...}
static async Task<bool> ProcessAsync()
{
var c = new Catalog();
var maxItems = Settings.GetInt("maxItems");
Logger.Info((maxItems == 0 ? "Processing all items" : "Processing first {0} items"), maxItems);
await c.ProcessCatalogAsync(maxItems);
return true; // This is not really used atm
}
}
public class Catalog
{
public async Task ProcessCatalogAsync(int maxItems)
{
var client = new Client();
var d = await client.GetFoldersAsync(_remoteFolder, maxItems);
var list = d as IList<string> ?? d.ToList();
Logger.Info("Found {0} folders", list.Count());
await ProcessFoldersAsync(list);
}
private async Task ProcessFoldersAsync(IEnumerable<string> list)
{
var client = new Client();
foreach (var mFolder in list.Select(folder => _folder + "/" + folder))
{
var items = await client.GetItemsAsync(mFolder);
var file = items.FirstOrDefault(n => n.ToLower().EndsWith(".xml"));
if (string.IsNullOrEmpty(file))
{
Logger.Warn("No metadata file found in {0}", mFolder);
continue;
}
await client.DownloadFileAsync(mFolder, file);
// Continue processing the received file...
}
}
}
public class Client
{
public async Task<IEnumerable<string>> GetItemsAsync(string subfolder)
{
return await GetFolderItemsAsync(subfolder, false);
}
public async Task<IEnumerable<string>> GetFoldersAsync(string subfolder, int maxItems)
{
var folders = await GetFolderItemsAsync(subfolder, true);
return maxItems == 0 ? folders : folders.Take(maxItems);
}
private async Task<IEnumerable<string>> GetFolderItemsAsync(string subfolder, bool onlyFolders)
{
// Downloads folder contents using WebRequest
}
public async Task DownloadFileAsync(string path, string file)
{
var remote = new Uri("ftp://" + _hostname + path + "/" + file);
var local = _workingFolder + #"\" + file;
using (var ftpClient = new WebClient { Credentials = new NetworkCredential(_username, _password) })
{
ftpClient.DownloadFileCompleted += (sender, e) => DownloadFileCompleted(sender, e, file);
await ftpClient.DownloadFileTaskAsync(remote, local);
}
}
public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e, Uri remote, string local)
{
if (e.Error != null)
{
Logger.Warn("Failed downloading\n\t{0}\n\t{1}", file, e.Error.Message);
return;
}
Logger.Info("Downloaded \n\t{1}", file);
}
}
Seems like some of your tasks aren't completed. Try do like this (I do the same when putting bunch of files to ftp)
Create array of tasks for every file being downloaded. Run them in cycle like this:
Task[] tArray = new Task[DictToUpload.Count];
foreach (var pair in DictToUpload)
{
tArray[i] = Task.Factory.StartNew(()=>{/* some stuff */});
}
await Task.WhenAll(tArray);
Use await Task.WhenAll(taskArray) instead of await each task. This guarantees all your tasks are completed.
Learn some peace of TPL ;)
I'm trying to make an epub parsing app in a Windows Store with C#, and it won't wait for the archive (epubs are actually zip files) to finish extracting before it tries to parse the not-yet-existing table of contents. How do I make my app be a bit more patient?
I've tried making my UnZip() function return a task and having the epub constructor (epub is a class) use UnZip().Wait(), but that just freezes the app. What do I do?
Edit: Here's my relevant code:
public class epub
{
public string filename;
private StorageFolder unzipFolder;
private IList<epubChapter> _contents;
private bool _parsed = false;
public bool parsed { get { return _parsed; } } //Epub and contents are fully parsed
public epub(string newFilename)
{
_contents = new List<epubChapter>();
filename = newFilename;
UnZipFile().Wait();
getTableOfContents();
}
private async Task UnZipFile()
{
var sourceFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
unzipFolder = await localFolder.CreateFolderAsync(filename, CreationCollisionOption.OpenIfExists);
using (var zipStream = await sourceFolder.OpenStreamForReadAsync(filename))
{
using (MemoryStream zipMemoryStream = new MemoryStream((int)zipStream.Length))
{
await zipStream.CopyToAsync(zipMemoryStream);
using (var archive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.Name != "")
{
using (Stream fileData = entry.Open())
{
try
{
await unzipFolder.GetFileAsync(entry.Name);
Debug.WriteLine("File at {0} already exists", entry.Name);
continue;
}
catch (FileNotFoundException)
{
Debug.WriteLine("Creating file {0}", entry.Name);
}
StorageFile outputFile = await unzipFolder.CreateFileAsync(entry.Name, CreationCollisionOption.OpenIfExists);
//Debug.WriteLine("Output file created at {0}", outputFile.Path);
using (Stream outputFileStream = await outputFile.OpenStreamForWriteAsync())
{
await fileData.CopyToAsync(outputFileStream);
await outputFileStream.FlushAsync();
}
}
if (entry.Name == "toc.ncx")
{
Debug.WriteLine("toc.ncx found in epub file; parsing it");
getTableOfContents();
}
}
}
}
}
}
}
public void getTableOfContents()
{
string contentsPath = unzipFolder.Path + #"\toc.ncx"; //The file is always called this in valid epubs
try
{
XDocument toc = XDocument.Load(contentsPath);
string nameSpace = getNameSpace(toc);
XElement navMap = firstElementNamed(toc.Root, "navMap");
parseNavPoints(navMap, nameSpace, 0);
_parsed = true;
}
catch(FileNotFoundException)
{
Debug.WriteLine("File toc.ncx was not found!");
}
}
Basically, your question seems to be: How do I call an async method from a constructor?
The short answer is that you don't, instead create an async factory method for your class.
Longer answer: As you noticed, if you call Wait(), your code will block. You can't use await, because constructors can't be async. And if you don't do anything, the constructor is going to return too early.
The solution here is to use an async factory method instead of a constructor. Something like:
private epub(string newFilename)
{
_contents = new List<epubChapter>();
filename = newFilename;
}
public static async Task<epub> CreateAsync(string newFilename)
{
var result = new epub(newFilename);
await result.UnZipFile();
result.getTableOfContents();
return result;
}
For some more information and alternative solutions, see Stephen Cleary's article about async and contructors.