Search code examples
c#visual-studiodirectorywebclient

Downloading from the internet link to the dynamically created folder. How is the path to this folder?


enter image description here

I am creating a dynamic folder as seen in the design in the image. I need to download two file types of data in a link to this folder I created, but how can I download it to the folder I created?

        klasor = textBox1.Text;
        var yol = Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"Beyza\source\" + klasor); 

In this way, I create a folder in the 'klasor' variable.

    public void indirButon_Click(object sender, EventArgs e)
    {

        string fileName = "C:\\Users\\Hilal Beyza\\Desktop\\projeler\\LinkProgram\\LinkProgram\\bin\\Debug\\Hilal Beyza\\yol\\webcams.mp4";
        WebClient web = new WebClient();
        web.DownloadFileCompleted += new AsyncCompletedEventHandler(Dosyaİndirme);
        Uri DosyaAdresi = new Uri(label3.Text);           
        web.DownloadFile(DosyaAdresi, fileName);
        
    }

I am giving my downloaded file a static path as above. How can I transfer this to the file I created?


Solution

  • Recommendations: When you write a program try to name components with a logic name, example txtUri, txtFolderName, btnDownload, this facilitates understanding.

    If you have a File Name and Dynamic Folder it's possible.

    public void indirButon_Click(object sender, EventArgs e)
    {        
        string fileName = GetFileName("webcams.mp4");
        WebClient web = new WebClient();
        web.DownloadFileCompleted += new AsyncCompletedEventHandler(DosyaIndirme);
        Uri DosyaAdresi = new Uri(label3.Text);
        web.DownloadFile(DosyaAdresi, fileName);
    }
    

    This method Get File Name with new Dynamicly Folder.

    private static string GetFileName(string fileName)
    {
        string klasor = textBox1.Text;
        string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Hilal Beyza\source\", klasor);
        if (!Directory.Exists(directoryPath))
            Directory.CreateDirectory(directoryPath);
    
        return Path.Combine(directoryPath, fileName);
    }
    

    If User pass complete path you need remove adapt the directoryPath

    string directoryPath = Path.Combine(textBox1.Text);