C
C#2y ago
.a

❔ My code cant write to a certain path

Basically i have a script to download a file from github into a specific folder and when i click the button that runs the code it tells me it doesnt have access to the path. Code. Using framework 4.8, c# visual form project
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;


namespace BPRINSTALLER
{
public partial class Form1 : Form

{
public Form1()
{
InitializeComponent();
}

private static byte[] DownloadFile(string url)
{
// Create a new WebClient object
using (var client = new WebClient())
{
// Use the WebClient's DownloadData method to download the contents of the specified URL
return client.DownloadData(url);
}
}

private void button2_Click(object sender, EventArgs e)
{
// Initialize a string variable with the URL of a file on GitHub
string fileUrl = "https://github.com/FowningFrog/burnout_mods/blob/main/350Z/VEH_PDSVK0B_AT.BIN";
// Call the DownloadFile method to download the contents of the file at the specified URL
byte[] fileContents = DownloadFile(fileUrl);
// Initialize a string variable with the file path where the downloaded file will be saved
string filePath = @"C:\Users\UKGC\Downloads\test";
// Use the WriteAllBytes method of the File class to write the contents of fileContents to the specified file path
File.WriteAllBytes(filePath, fileContents);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;


namespace BPRINSTALLER
{
public partial class Form1 : Form

{
public Form1()
{
InitializeComponent();
}

private static byte[] DownloadFile(string url)
{
// Create a new WebClient object
using (var client = new WebClient())
{
// Use the WebClient's DownloadData method to download the contents of the specified URL
return client.DownloadData(url);
}
}

private void button2_Click(object sender, EventArgs e)
{
// Initialize a string variable with the URL of a file on GitHub
string fileUrl = "https://github.com/FowningFrog/burnout_mods/blob/main/350Z/VEH_PDSVK0B_AT.BIN";
// Call the DownloadFile method to download the contents of the file at the specified URL
byte[] fileContents = DownloadFile(fileUrl);
// Initialize a string variable with the file path where the downloaded file will be saved
string filePath = @"C:\Users\UKGC\Downloads\test";
// Use the WriteAllBytes method of the File class to write the contents of fileContents to the specified file path
File.WriteAllBytes(filePath, fileContents);
}
}
}
Any help is appreciated <3
11 Replies
Malibloo
Malibloo2y ago
Is test the folder or the filename? If it's the folder, you should add the filename on WriteAllBytes' path variable. If it's actually the filename despite the lacking extension, check if there's not a folder of the same name.
.a
.a2y ago
Test is the name of the folder that i want to download the github file into
Malibloo
Malibloo2y ago
Then you need to add an actual filename to filePath. You could use Path.GetFileName on the fileUrl and paste it onto the filePath if you want it to have the same name.
.a
.a2y ago
how would i do that? im a beginner but the actual file name is Test
Malibloo
Malibloo2y ago
Is it the file name or the folder? You just said it's both.
.a
.a2y ago
The name test is the name of the folder the file name im getting from github is VEH_PDSVK0B_AT.BIN
Malibloo
Malibloo2y ago
Right. So you get that filename by using Path.GetFileName(), and then past that onto the end of your location. WriteAllBytes need a full path plus the filename. It doesn't just guess what the file is named.
.a
.a2y ago
So what would the code look like then? its hard for me to understand without examples sorry 😅
Malibloo
Malibloo2y ago
I'm not sure what the rules are here about spoonfeeding, but I'm not going to write your code for you, sorry. What you need was explained in my second and fourth message.
.a
.a2y ago
oh it worked thank you for your time and hassle when
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.