Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从联机文件中获取文本。Text_C#_Winforms_Notifications_Push Notification - Fatal编程技术网

C# 如何从联机文件中获取文本。Text

C# 如何从联机文件中获取文本。Text,c#,winforms,notifications,push-notification,C#,Winforms,Notifications,Push Notification,我正在使用C#创建一个应用程序,该应用程序应该以文本形式从以下位置的文件(在线找到)下载数据: 我只需要获取文本数据。到目前为止,我的代码是: // this.Hide(); // notifyIcon1.Visible = true; WebClient Get= new WebClient(); Uri Line = new Uri("http://asap.eb2a.com/a.text"); AAA: var text = Get.DownloadData(Line); // var

我正在使用C#创建一个应用程序,该应用程序应该以文本形式从以下位置的文件(在线找到)下载数据:

我只需要获取文本数据。到目前为止,我的代码是:

//  this.Hide();
//  notifyIcon1.Visible = true;
WebClient Get= new WebClient();
Uri Line = new Uri("http://asap.eb2a.com/a.text");
AAA:
var text = Get.DownloadData(Line);
// var text = System.IO.File.ReadAllText(@"D:\b.txt");
//System.IO.File.Delete(@"D:\Not.text");
label1.Text = text.ToString();
while (text.ToString() == text.ToString())
try
{
    notifyIcon1.Visible = true;
    notifyIcon1.BalloonTipText = (Convert.ToString(text));
    notifyIcon1.BalloonTipTitle = "We Notify You";
    notifyIcon1.ShowBalloonTip(150);
    BBB:   var text1  = Get.DownloadData(Line);
    //System.IO.File.ReadAllText(@"D:\b.txt");
    while(text.ToString()==text1.ToString())
    {
        await Task.Delay(50);
        goto BBB;
    }
    await Task.Delay(50);
    goto AAA; 
}
catch
{
    MessageBox.Show("Error");
}

为了确保数据不一样,我做了一些循环,在Notify中得到了这一点,我假设您的问题是Notify图标文本中显示的HTML文本。可能网站(asap.eb2a.com…)不允许未知代理,因此它会返回一些消息通知您,或者
a.text
文件包含HTML和JavaScript代码。第一个问题可以通过添加屏蔽来绕过,就像来自web浏览器一样:

Get.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
使用
goto/label
构造创建的循环可以替换为。这也将简化您的代码:

  • 创建WebClient对象以调用uri
  • 创建一个变量以保留下载的文本
  • 创建计时器对象并绑定事件,即每隔
    100
    毫秒调用一次
  • 在计时器已过事件中,使用以下方法从站点下载数据
  • 将已保存的文本与新下载的字符串进行比较:如果它们不同,则显示通知图标
实现如下所示:

WebClient Get = new WebClient();
// identify your self as a web browser
Get.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Uri uri = Uri("http://asap.eb2a.com/a.text");
string contentToShow = String.Empty;
// timer for the repetitive task called every nnn milliseconds
const long refreshIntervalInMilliseconds = 100;
System.Timers.Timer timer = new System.Timers.Timer(refreshIntervalInMilliseconds);
timer.Elapsed += (s, e) =>
{
    Console.WriteLine(String.Format("{0} Fetching data from: {1}", DateTime.Now, uri));
    try
    {           
        var result = Get.DownloadString(uri);
        if (result != contentToShow)
        {
            contentToShow = result;
            notifyIcon1.Visible = true;
            notifyIcon1.BalloonTipText = contentToShow;
            notifyIcon1.BalloonTipTitle = "We Notify You";
            notifyIcon1.ShowBalloonTip(150);
        }
    }
    catch (Exception exception)
    {
        MessageBox.Show(string.Format("Error: {0}", exception.Message));
    }
};
// start the timer
timer.Start();

请,请,请,请不要使用!你想做的事情可以不用标签和goto操作符。你知道elser吗@pastyWhy您需要
任务。延迟(50)?为什么需要
while
循环?您可以使用
DownloadString
(一次下载完整的字符串),而不是
DownloadData
。您到底想做什么?是否只需要响应中的文本?您的问题是否与notify消息中显示的不需要的HTML代码有关?如果是这样的话,那么请查看调试程序中的结果,以了解获取的repsonse为何不像预期的那样出现。可能网站包含了一条解释问题的信息。