3

I created a script to extract an executable file from the Resources to my Desktop. This works on my machine, but won't work on other's people machines because of a different username. The following script works perfectly:

private void button1_Click(object sender, EventArgs e)
{
    byte[] myfile = Properties.Resources.SOMETHING;
    File.WriteAllBytes("C:\\Users\\Alex\\Desktop\\SOMETHING.exe",myfile);
}

I did some some research and found that I need to use the (Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

So I compiled this script:

private void button1_Click(object sender, EventArgs e)
{
    byte[] myfile = Properties.Resources.SOMETHING;
    File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),myfile);
}

The problem is that it doesn't say there is an error in my code, but when I run it and press on the button I get the following error:

System.UnauthorizedAccessException Message=Access to the path 'C:\Users\Alex\Desktop' is denied.

I've tried running the code with administrator rights, but that also didn't help.

2
  • You try to write the bytes to a file having the same name as the Desktop folder. This does not work. Try File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "myfilename.txt"), myfile); Commented Mar 10, 2019 at 19:23
  • Have they tried running it as an admin? Commented Mar 10, 2019 at 22:55

1 Answer 1

1

Use File.SetAttributes(myfile, FileAttributes.Normal); property before reading the file, it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

It states the following Cannot convert Byte to string
byte[] myfile = Properties.Resources.SOMETHING; string result = System.Text.Encoding.UTF8.GetString(myfile); File.SetAttributes(result , FileAttributes.Normal); File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),result ); try this it should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.