C# 如何获取网站上文件夹的名称

C# 如何获取网站上文件夹的名称,c#,webclient-download,C#,Webclient Download,我正在寻找一种方法来获取位于(例如)http://example.com/download/“foldername在此” 这是因为我有一个c#程序,如果你没有准备好,它总是下载.zip,但目前它不会下载新版本 示例:当前它从http://example.com/download/1.0/file.zip,如果您准备好了它检查的文件夹,它将不会下载,但如果您确实有该文件夹,它将不会从http://example.com/download/2.0/file.zip,其中包含新版本。我制作了一个包含当

我正在寻找一种方法来获取位于(例如)
http://example.com/download/“foldername在此”

这是因为我有一个c#程序,如果你没有准备好,它总是下载.zip,但目前它不会下载新版本

示例:当前它从
http://example.com/download/1.0/file.zip
,如果您准备好了它检查的文件夹,它将不会下载,但如果您确实有该文件夹,它将不会从
http://example.com/download/2.0/file.zip
,其中包含新版本。我制作了一个包含当前版本的文件,但是如何让我的程序对照最新版本所在的文件夹检查该文件

编辑:我重新编写了代码,让它先下载
version.txt
文件,然后读取包含最新下载版本的内容

代码:


目前这是可行的,但我很确定这段代码可以改进,或者整个方法可以改进到如何获取最新版本。你可以有一个文本文件,包含最新版本号以及从哪里下载,放在一个永久的位置,比如说
http://example.com/download/latestversion.txt
。然后使用
WebClient.DownloadString
检索该文件,并根据文件内容采取适当的操作。

Url路径不必映射到物理路径。对于ex
http://foo.bar/download/mostrecent/
今天可以映射到
3.0
,但明天可以映射到
4.0
。是否获取
http://foo.bar/download/
?@I4V我一直希望能有这样的东西,不过我怎样才能让它真正起作用呢?通过制作一个额外的文件夹重定向到最新版本?@DmitryDovgopoly,我想这将是一种未来的可能性,这样人们就可以选择他们想要的版本,但现在我只想让它找到最新版本。在这种情况下,安德鲁的答案是唯一的选择。如果没有在某个地方写入文件夹名,则无法找到该文件夹名。它可以是一个文本文件或html页面。这是一个选项,但这不是需要很长的时间吗?我很确定有一种更简单的方法,不需要额外的临时下载。
if (radUseFile.Checked)
{
//get latest version on website
WebClient modver = new WebClient();
modver.UseDefaultCredentials = true;
modver.DownloadFile("http://www.example.com/download/version.txt", tempdir _
+ "version.txt");
using (StreamReader ver = new StreamReader(tempdir + "version.txt"))
{
    siteversion = ver.ReadToEnd();
}
File.Delete(tempdir + "version.txt");
if (Directory.Exists(folder))
{
    if (File.Exists(folder + "\\version.txt"))
    {
        using (StreamReader sr = new StreamReader(folder + "\\version.txt"))
        {
            latestversion = sr.ReadToEnd();
        }
        if (latestversion != siteversion)
        {
            uptodate = false;
        }
        else
        {
            uptodate = true;
        }
    }
}
else
{
    uptodate = false;
}
if (!uptodate)
{
    //download and extract the files
    WebClient downloader = new WebClient();
    label4.Text = "Downloading full client. May take a while";
    this.Update();
    downloader.UseDefaultCredentials = true;
    downloader.DownloadFile("http://www.example.com/download" + siteversion _
+ "/zipfile.zip", templocation + "zipfile.zip");

    label4.Text = "Extracting...";
    Shell32.Shell sc = new Shell32.Shell();
    Directory.CreateDirectory(tempdir);
    Shell32.Folder output = sc.NameSpace(tempdir);
    Shell32.Folder input = sc.NameSpace(tempdir + "zipfile.zip");
    output.CopyHere(input.Items(), 4);

    label4.Text = "Cleaning up...";
    File.Delete(tempdir + "zipfile.zip");

    new Microsoft.VisualBasic.Devices.Computer().FileSystem.CopyDirectory(tempdir,_
folderlocation, true);

    Directory.Delete(tempdir, true);

    uptodate = true;
}
}
else
{
uptodate = true;
}