Reputation: 255
I have a Winforms Auto Updater.
If I open the program it will get the file download link from a raw text which contains the .zip file download link to the program from the web.
But if the person made a folder and put the program in the folder I want the program to get the folder name the program is in and extract the zip file to it.
Here's my code:
private void StartDownload()
{
WebClient webClient = new WebClient();
string path = "./Sympathy";
string address1 = "https://ghostbin.co/paste/cjx7j/raw";
string address2 = webClient.DownloadString(address1);
this.progressBar1.Value = 100;
string str = "./Sympathy.zip";
if (System.IO.File.Exists(str))
{
System.IO.File.Delete(str);
}
else
{
webClient.DownloadFile(address2, str);
ZipFile.ExtractToDirectory(str, Directory.GetCurrentDirectory(Directory).Name);
System.IO.File.Delete(str);
}
}
But I'm having an error on the (Directory)
part, How do I fix it?
Upvotes: -2
Views: 732
Reputation: 34
To get the path to your executable use System.Reflection.Assembly.GetEntryAssembly().Location;
, to get the directory use Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
I see that your code is flawed. Use this instead, to do everything :
string archivePath = "Sympathy.zip";
using (var client = new WebClient())
{
client.DownloadFile(client.DownloadString("https://ghostbin.co/paste/cjx7j/raw"), archivePath);
ZipFile.ExtractToDirectory(archivePath, Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
File.Delete(archivePath);
}
It works. It downloads a bunch of unknown files.
Upvotes: 1
Reputation: 432
What is the error that you receive? That will help with the issue that you have.
You mention that you get an error on the (Directory)
part of your code, there are some things that I see on this line:
Directory.GetCurrentDirectory
doesn't expect an input;Directory.GetCurrentDirectory()
returns a string, you can't call the property Name
on this.Be aware that the result of this call is not always where your application is residing. If you check the properties of a hyperlink, you can set the Target
' and the Start in
. The Target
is your program and the Start in
is the current directory that is provided to your application. Instead of creating a hyperlink, you can also open a command prompt (defaults to your home directory) and type the full path to your application, this will cause your application to open and receive as current directory the path where your command prompt was pointing to (in case of the default, your home directory). The answer in post Get current folder path goes a bit deeper in detail about this.
Upvotes: 1
Reputation: 1
With this Line You Can Get The Current Directory of Your Application.
Application.StartupPath;
it gives you the Folder of your .exe running app.
Upvotes: 0