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!