从外部文件插入命令 [英] insert commands from external file
问题描述
大家好,
我有一个带有一个列表和一个Web浏览器以及一个Web上外部文件的应用程序
可以这样说:
我的应用程式"C:\ App.exe"
我的文件"Http:\\ something.com \ file.txt"
文件内容:
Hello everyone,
i have an app with one list and one webbrowser and one external file on the web
lets say this:
my app "C:\App.exe"
my file "Http:\\something.com\file.txt"
the content of the file:
[Title]
Google
Facebook
[URL]
http://www.google.com
http://facebook.com
现在如何使应用程序读取文本文件
然后在列表中包含标题"对象
然后为每个人设置"OnClick"事件,
now how can i make the app read the text file
then include the "Title" objects in the list
then make "OnClick" event for every one as this one
WebBrowser1.Navigate("http://xxxx.xxx")
而且必须像这样:
在列表上,第一个对象将是"Google"
和"OnClick"事件将是"
and this have to be like this:
on the list the first object will be "Google"
and "OnClick" event will be "
WebBrowser1.Navigate("http://www.google.com")
所以,这可能与否
如果可能的话,请帮助我:(
so, is this possible or not
if it possible please help me to do it :(
推荐答案
当然可以!
我个人希望将网站的标题和网址存储在同一行.
示例:
Google | http://www.google.com/
脸谱网| http://www.facebook.com/
这样一来,解析文件就会更加容易.
例如,我将把这个文件解析成一个字典,标题是键,URL是值.
代码在C#中,然后我意识到您想要VB,所以我使用 http://converter.telerik.com/ [^ ]
///C#代码///
Sure it is possible!
Personally I would want to store the sites title and url on the same line though.
EXAMPLE:
Google|http://www.google.com/
Facebook|http://www.facebook.com/
Then it would be even easier to parse the file.
For an example I will parse this file into a dictionary with the title being the key and the url being the value.
The code was in C# then I realized you wanted VB so I convert the code using the converter at http://converter.telerik.com/[^]
/// C# Code ///
using (WebClient client = new WebClient())
{
client.DownloadFile("http://something.com/test.txt", "test.txt");
}
string[] lines = File.ReadAllLines(@"test.txt");
Dictionary<string, string> fileData = new Dictionary<string, string>();
for (int i = 0; i < lines.Length; i++)
{
string[] split = lines[i].Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries);
fileData.Add(split[0], split[1]);
}
///VB.Net代码///
/// VB.Net Code ///
Using client As New WebClient()
client.DownloadFile("http://something.com/test.txt", "test.txt")
End Using
Dim lines As String() = File.ReadAllLines("test.txt")
Dim fileData As New Dictionary(Of String, String)()
For i As Integer = 0 To lines.Length - 1
Dim split As String() = lines(i).Split(New Char() {"|"C}, StringSplitOptions.RemoveEmptyEntries)
fileData.Add(split(0), split(1))
Next
然后只是为了展示它的工作原理.
///C#代码///
Then just to show it works.
/// C# Code ///
string title = fileData.Keys.ElementAt(0);
string url = fileData[title];
MessageBox.Show(title + " - " + url);
///VB.Net代码///
/// VB.Net Code ///
Dim title As String = fileData.Keys.ElementAt(0)
Dim url As String = fileData(title)
MessageBox.Show(title + " - " + url)
该消息框将显示"Google-http://www.google.com/"
您可以将所有这些键添加到listBox中,例如:
///C#代码///
the message box would show " Google - http://www.google.com/ "
You could add all these keys to a listBox for example:
/// C# Code ///
for (int i = 0; i < fileData.Count; i++)
{
sitesListBox.Items.Add(fileData.Keys.ElementAt(i));
}
///VB.Net代码///
/// VB.Net Code ///
For i As Integer = 0 To fileData.Count - 1
sitesListBox.Items.Add(fileData.Keys.ElementAt(i))
Next
然后使用列表框的SelectedIndexChanged事件将Web浏览器导航到url
///C#代码///
then use the SelectedIndexChanged event of the list box to navigate the web browser to the url
/// C# Code ///
private void sitesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser.Navigate(fileData[(string)sitesListBox.SelectedItem]);
}
///VB.Net代码///
/// VB.Net Code ///
Private Sub sitesListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sitesListBox.SelectedIndexChanged
webBrowser.Navigate(fileData(DirectCast(sitesListBox.SelectedItem, String)))
End Sub
注意:
有很多方法可以做到这一点,但这是一种可以正常工作的快速方法.
NOTE:
There are many ways to do this but this is a quick way that will work just fine.
这篇关于从外部文件插入命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!