-2

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?

TERIHAX
  • 237
  • 2
  • 14

3 Answers3

1

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.

  • The error is: CS0119 'Directory' is a type, which is not valid in the given context – TERIHAX Aug 01 '20 at 12:49
  • https://stackoverflow.com/q/15653921/3107892 isn't what I want. – TERIHAX Aug 01 '20 at 12:54
  • This goes to point 2 of my answer, you have typed `Directory` and are using it as an input. The issue is that you can't use a class as input, only an instance of a class/type. Removing this text will solve the issues that you have (and the next issue will be the `.Name` that is unknown, also remove this) – Christophe Devos Aug 01 '20 at 20:05
1

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
anti
  • 34
  • 4
0

With this Line You Can Get The Current Directory of Your Application.

Application.StartupPath;

it gives you the Folder of your .exe running app.