wpf 从外部文件检索链接

velaa5lx  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(159)

我正在开发一个GUI,它的主要用途是从Web上下载工具。
该应用程序预计为.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");
        }

    }

}
kulphzqa

kulphzqa1#

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

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

并在您的应用程序中检索它:
新的Web客户端();

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,可以使用repository模式

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");
}

相关问题