F
F
Fenion2020-05-10 18:04:27
C++ / C#
Fenion, 2020-05-10 18:04:27

Is it possible to create a directory if part of the path is missing when downloading files?

Hello, I am writing a game launcher in C#. There was a problem when checking the integrity of client files, the launcher notices the absence of a certain file and downloads the necessary file, however, if you delete part of the path to the file, the launcher cannot load it there (System.IO.DirectoryNotFoundException: Could not find part of the path). I've been doing C# for 2 days, hence the difficulty. Here is the code

private void CheckMeta()
        {
            string api_adress = "http://localhost/api";
            string clientFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\aaaa\";
            WebRequest request = WebRequest.Create(api_adress + "/meta-inf");
            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";
            request.Proxy = new WebProxy();
            Stream res = request.GetResponse().GetResponseStream();
            StreamReader read = new StreamReader(res);
            string meta = read.ReadToEnd();
            var metas = JsonConvert.DeserializeObject<Meta_obj[]>(meta);
            foreach (var Meta_obj in metas)
            {
                if (!File.Exists(clientFolder + Meta_obj.Artefact))
                {
                    progressBar1.Visible = true;
                    dwn_text.Visible = true;
                    WebClient client = new WebClient();
                    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgress);
                    client.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadDone);
                    client.DownloadFile(
                            new System.Uri("http://localhost:3001" + Meta_obj.Src),
                            clientFolder + Meta_obj.Artefact
                        );
                }
                else
                {
                    if (Meta_obj.Check_hash)
                    {
                        if (this.CheckHash(clientFolder + Meta_obj.Artefact) != Meta_obj.Hash)
                        {
                            progressBar1.Visible = true;
                            dwn_text.Visible = true;
                            WebClient client = new WebClient();
                            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgress);
                            client.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadDone);
                            client.DownloadFileAsync(
                                new System.Uri("http://localhost:3001" + Meta_obj.Src),
                                clientFolder + Meta_obj.Artefact
                            );
                        }
                    }
                }
            }
            read.Close();
            this.ClientStart();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fenion, 2020-05-10
@Fenion

I figured it out, Path.GetDirectoryName() helped with this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question