Wpf 从外部文件检索链接
我正在开发一个GUI,它的主要用途是从web下载工具。Wpf 从外部文件检索链接,wpf,text,hyperlink,download,Wpf,Text,Hyperlink,Download,我正在开发一个GUI,它的主要用途是从web下载工具。 该应用程序是针对.Net Framework 3.5(兼容性)设计的,到目前为止效果很好,但在我的脑海中闪现了以下问题:每次这些应用程序中的一个更改链接时,我也必须在我的项目中更改它,以便它能够反映最新版本/链接 有没有可能从本地文本文件或更好的pastbin/googledoc中读取链接,以便我可以从外部修改链接 希望它能像在txt文件中放入string ccleaner=“www.ccleaner.link等一样简单,然后用file.R
该应用程序是针对.Net Framework 3.5(兼容性)设计的,到目前为止效果很好,但在我的脑海中闪现了以下问题:每次这些应用程序中的一个更改链接时,我也必须在我的项目中更改它,以便它能够反映最新版本/链接 有没有可能从本地文本文件或更好的pastbin/googledoc中读取链接,以便我可以从外部修改链接 希望它能像在txt文件中放入
string ccleaner=“www.ccleaner.link
等一样简单,然后用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"];
}
}
}
名称空间myapp
{
///
///App.xaml的交互逻辑
///
公共部分类应用程序:应用程序
{
公共无效应用程序\u启动(对象发送器、StartupEventArgs e)
{
var wc=新的WebClient();
var csv=wc.DownloadString(“https://docs.google.com/spreadsheets/d/1IjQfWMIQyw8NuncRd91iWJD_GdWTCqrrX11pTBv1bEA/edit?usp=sharing");
var links=csv
.Split('\n')//提取行
.Skip(1)//跳过标题行
.Select(line=>line.Split(“,”)//将应用程序名与下载URL分开
.ToDictionary(令牌=>令牌[0],令牌=>令牌[1]);
var CCleanerLink=链接[“CCleaner”];
}
}
}
cs(这是主窗口中的一个页面)
名称空间myapp
{
///
///tools.xaml的交互逻辑
///
公共部分类工具:第页
{
公共工具()
{
初始化组件();
}
公共无效下载文件(字符串地址、字符串文件名)
{
WebClient down=新建WebClient();
添加(HttpRequestHeader.UserAgent),Mozilla/5.0(兼容;http://example.org/)");
ServicePointManager.SecurityProtocol=(SecurityProtocolType)3072;
DownloadFileAsync(新Uri(地址)、文件名);
}
私人无效自动检查(对象发送者,路由目标e)
{
下载文件(“https://live.sysinternals.com/autoruns.exe“,“autoruns.exe”);
}
已选中私有无效清除器(对象发送器,路由目标e)
{
下载文件(CCleanerLink,“ccleaner.exe”);
}
}
}
您可以将最新链接存储在服务器上存储的CSV文件中,其格式类似于:
Application,URL
CCleaner,https://www.ccleaner.com/fr-fr/ccleaner/download
...,...
...,...
...,...
并在应用程序中检索它:
var wc=新的WebClient()
要干净地管理应用程序中的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;
}
}
是的,这是可能的,但你的问题(按现状)并非如此;请检查我应该将其放置在何处,以便链接本身可以通过某种方法访问?CSV文件可以托管在任何将其公开为公共web资源的系统上。例如,你可以通过调整URL,使其以
https://docs.google.com/uc?export=download&id=your_CSV_file_Google_Drive_ID
。对不起,我不是很清楚。我刚才说的是应用程序代码。我应该把它放在哪里,以便通过方法访问它?例如DownloadFileAsync(CCleanerLink,“ccleaner.exe”);你可以把它放在应用程序.Startup
事件处理程序中。不要认为我做得不对。。。当前上下文中不存在名称“CCleanerLink”
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"];
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");
}