2
Well, I’m trying to develop an automatic setup for a program I developed. First, it would create a folder in C:/Filename; then it would download a Github repository as .zip in this folder, then export the zip to it and finally remove .zip. Missing add something to run 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 code from Download someone else.
This is the code, but when executing it, it returns me the following error:
System.UnauthorizedAccessException: O acesso ao caminho 'C:/NomeDaPasta' foi negado.
I’ve looked for help, but I didn’t succeed. Please, someone help me!
Have you ever tried to do the process in another directory without being at the root of C://Drive? Type try to do in another folder outside of C:/Filename, as you are doing.
– Wesley Heron
Have you tried to give permission in the folder?
– Jéf Bueno
@Wesleyheron would even do but how the path would be set, since it can vary from pc to pc? If you have any way please tell me!
– Pedro Brito
@jbueno followed a tutorial to add permission to the program using visual studio( the IDE I use ) apparently should work but not.
– Pedro Brito
Why not add permission using Windows itself? Right-click on the folder > Estates > Security, etc..
– Jéf Bueno
but that wouldn’t apply to every computer, everyone who wanted to download my program would have to do this, and I don’t want that.
– Pedro Brito
See if it helps https://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol(v=vs.110). aspx
– Marco Souza