Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

c# - 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 + @"Hilal Beyzasource" + 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 Beyzasource", 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...