Access denied exception using C# / UWP

爱⌒轻易说出口 提交于 2019-12-14 04:09:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!