System.UnauthorizedAccessException in C #

1

Well, I'm trying to develop an automatic setup for a program I've developed. First, it would create a folder in C: / FolderName; Then it would drop a Github repository like .zip into this folder, then export the zip to it and finally remove the .zip. There is something missing to run the setup, but the problem is not there.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace C_Sharp_WIndowsFormTest
{
    class setup
    {
public static void download(string url, string path, string githubToken)
    {

        using (var client = new System.Net.Http.HttpClient())
        {
            var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", githubToken);
            credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
            var contents = client.GetByteArrayAsync(url).Result;
            System.IO.File.WriteAllBytes(path, contents);
        }

    }

    public static void JustDoIt()
    {

        string directory = "C:/NomeDaPasta";
        string githubToken = "MeuGithubToken";
        string url = "UrlDoRepositório";

        if (!Directory.Exists(directory))
        {

            /*## Cria a Pasta##*/

            Directory.CreateDirectory(directory);
            download(url, directory, githubToken);
            ZipFile.ExtractToDirectory("C:/NomeDaPasta/Repositório.zip", directory);

            /*## Deleta o zip ##*/
            File.SetAttributes(directory, FileAttributes.Normal);
            File.Delete("C:/NomeDaPasta/Repositório.zip");

            /*## Final (Deleta a pasta) ##*/
            Directory.Delete(directory);


        }
        else
        {

            MessageBox.Show("Folder already exists!");
            Directory.Delete(directory);
            JustDoIt();

        }

    }

    static void Main(string[] args)
    {

        try
        {

            JustDoIt();             

        } catch (Exception e)
        {

            MessageBox.Show(e.ToString());

        }

    }

}

}

NOTE: I just copied the download code from other person .

This is the code, but when I run it, it returns the following error:

System.UnauthorizedAccessException: O acesso ao caminho 'C:/NomeDaPasta' foi negado.

I've already tried to get help, but I was not successful. Please, somebody help me!

    
asked by anonymous 09.11.2016 / 17:48

3 answers

1

You can run your application as an administrator. Incidentally, you can indicate that the application should be run only with administrator rights. For this you have to create a manifest for your application.

Andyouhavetochangepermissionsforadmin

<requestedExecutionLevellevel="requireAdministrator" uiAccess="false" />

Font

To avoid running your application as an administrator type in another site such as

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    
17.11.2016 / 20:00
0

Replace these points:

string directory = @"C:\NomeDaPasta";

string caminhoZip = @"C:\NomeDaPasta\Repositório.zip";

ZipFile.ExtractToDirectory(caminhoZip, directory);

File.Delete(caminhoZip);

    
10.03.2018 / 02:47
0

You need to edit the folder security options because the process that is calling the folder creation and delete functions does not have the Read and / or Write credentials to perform such an operation.

You can confirm this by switching the path to a generic test:

string directory = @"C:\Users\Public\Teste";

With this path (which by the way is more accessible), you will be able to generate what you need.

    
03.10.2018 / 03:13