TERIHAX
TERIHAX

Reputation: 255

How to get the program's current folder name and extract the zip file to the current folder in C#?

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

Answers (3)

anti
anti

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

Christophe Devos
Christophe Devos

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:

  1. The method Directory.GetCurrentDirectory doesn't expect an input;
  2. You can't pass a type as a parameter;
  3. The call 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

Ali Mehdipour
Ali Mehdipour

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

Related Questions