从外部文件检索链接

时间:2018-11-26 13:44:21

标签: wpf text hyperlink download

我正在开发一个GUI,其主要用途是从网络上下载工具。
该应用程序计划用于.Net Framework 3.5(出于兼容性考虑),到目前为止运行良好,但是在我看来,这引发了以下问题:每当这些应用程序之一更改链接时,我也必须在项目中对其进行更改,以便它可以反映出最新版本/链接。

是否可以从本地文本文件中读取链接,甚至可以从pastbin / googledoc中读取链接,以便我可以从外部修改链接?

希望这就像将string ccleaner = "www.ccleaner.link等放入txt文件并用File.ReadAllText读取一样简单... App.xaml.cs:

namespace myapp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public void Application_Startup(object sender, StartupEventArgs e)
        {
            var wc = new WebClient();
            var csv = wc.DownloadString("https://docs.google.com/spreadsheets/d/1IjQfWMIQyw8NuncRd91iWJD_GdWTCqrrX11pTBv1bEA/edit?usp=sharing");
            var links = csv
                .Split('\n') // Extract lines
                .Skip(1) // Skip headers line
                .Select(line => line.Split(',')) // Separate application name from download URL
                .ToDictionary(tokens => tokens[0], tokens => tokens[1]);
            var CCleanerLink = links["CCleaner"];
        }
    }
}

tools.xaml.cs(这是主窗口中的页面)

namespace myapp
{
    /// <summary>
    /// Interaction logic for tools.xaml
    /// </summary>
    public partial class tools : Page
    {
        public tools()
        {
            InitializeComponent();

        }


        public void downloadFile(String address, String filename)
        {
            WebClient down = new WebClient();
            down.Headers.Add(HttpRequestHeader.UserAgent,"Mozilla/5.0 (compatible; http://example.org/)");
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            down.DownloadFileAsync(new Uri(address), filename);
        }

        private void Autor_Checked(object sender, RoutedEventArgs e)
        {
            downloadFile("https://live.sysinternals.com/autoruns.exe", "autoruns.exe");

        }

        private void Ccleaner_Checked(object sender, RoutedEventArgs e)
        {
            downloadFile(CCleanerLink, "ccleaner.exe");
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您可以将最新链接存储在服务器上存储的CSV文件中,其格式类似于:

Application,URL
CCleaner,https://www.ccleaner.com/fr-fr/ccleaner/download
...,...
...,...
...,...

然后在您的应用程序中检索它:

var wc = new WebClient();

var csv = wc.DownloadString("https://docs.google.com/spreadsheets/d/1IjQfWMIQyw8NuncRd91iWJD_GdWTCqrrX11pTBv1bEA/gviz/tq?tqx=out:csv&sheet=Sheet1");
var links = csv
    .Split('\n') // Extract lines
    .Skip(1) // Skip headers line
    .Where(line => line != "") // Remove empty lines
    .Select(line => line.Split(',')) // Separate application name from download URL
    .ToDictionary(tokens => tokens[0].Trim('"'), tokens => tokens[1].Trim('"'));
var CCleanerLink = links["CCleaner"];

要干净地管理应用程序中的URI,可以使用存储库模式

public static class ApplicationsRepository
{
    private static IDictionary<string, string> URIs = null;

    public static IDictionary<string, string> GetAllURIs()
    {
        if (URIs == null)
        {
            var wc = new WebClient();
            var csv = wc.DownloadString("http://myhosting.com/application/tools-downloader/config/links.csv");
            URIs = csv
                .Split('\n')
                .Skip(1)
                .Select(line => line.Split(','))
                .ToDictionary(tokens => tokens[0], tokens => tokens[1]);
        }

        return URIs;
    }

    public static string GetURI(string applicationName)
    {
        var allURIs = GetAllURIs();

        string applicationURI = null;
        allURIs.TryGetValue(applicationName, out applicationURI);

        return applicationURI;
    }
}

然后在您的事件处理程序中:

private void Ccleaner_Checked(object sender, RoutedEventArgs e)
{
    downloadFile(ApplicationsRepository.GetURI("CCleaner"), "ccleaner.exe");
}