问题
The execute()
function is linked to a button press.
The first time it is pressed - everything runs the way it should/
The second time is is pressed (within the same execution of the app; if I close and re-open it works fine) - on the DeleteAsync
line I get this exception:
An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
I think something is staying open longer than it should, hence the error. But I'm not sure what is causing it exactly. If anyone can see from the code below what could be happening I would be most grateful:
private async void execute(string url)
{
StorageFolder filesFolder = null;
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
if (await localFolder.TryGetItemAsync(DIRECTORY_NAME) != null)
{
filesFolder = await localFolder.GetFolderAsync(DIRECTORY_NAME);
await filesFolder.DeleteAsync(StorageDeleteOption.PermanentDelete); // << THIS IS THE EXCEPTION LINE
}
if (await localFolder.TryGetItemAsync(DIRECTORY_NAME) == null)
{
await Task.Run(() => {
repository.Clone(url, DIRECTORY_NAME);
});
}
// This won't be called until task above finished running
filesFolder = await localFolder.GetFolderAsync(DIRECTORY_NAME);
IEnumerable<StorageFile> files = await filesFolder.GetFilesAsync();
addFiles(files);
}
Clone function:
void Repository::Clone(Platform::String^ url, Platform::String^ path)
{
OutputDebugString(L"Clone ");
OutputDebugString(url->Data());
OutputDebugString(L" into ");
char temp[1024];
GetLocalPath(path, temp, 1024);
OutputDebugStringA(temp);
OutputDebugString(L"\n");
char urlt[1024];
wcstombs(urlt, url->Data(), 1024);
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
// Set up options
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
checkout_opts.progress_cb = on_checkout_progress;
clone_opts.checkout_opts = checkout_opts;
clone_opts.fetch_opts.callbacks.sideband_progress = on_sideband_progress;
clone_opts.fetch_opts.callbacks.transfer_progress = on_fetch_transfer_progress;
clone_opts.fetch_opts.callbacks.credentials = on_credentials_required;
clone_opts.fetch_opts.callbacks.payload = this->native;
// Start the cloning
report_error(git_clone(&repo, urlt, temp, &clone_opts));
}
来源:https://stackoverflow.com/questions/38988937/access-denied-exception-using-c-sharp-uwp