Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Interop;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using Image = System.Windows.Controls.Image;
- namespace Cursach_1._3
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- string[,] rssData = null;
- string urlfinder;
- Dictionary<string, string> Channals = new Diction-ary<string, string>();
- Dictionary<int, string> chanalstack = new Diction-ary<int, string>();
- public string ProgramPath = Directo-ry.GetCurrentDirectory();
- public MainWindow()
- {
- InitializeComponent();
- var lines = File.ReadAllLines(ProgramPath + "\\" +"Chanals.txt"); // читаем построчно
- foreach (var line in lines)
- {
- var tokens = line.Split('=');
- var key = tokens[0].Trim();
- var value = string.Join("", to-kens.Skip(1)).Trim();
- Channals[key] = value;
- }
- int i = 0;
- foreach (string finder in Channals.Keys)
- {
- StackPanel stackPanel = new StackPanel { Width = 100, Height = 50 };
- Label label = new Label { Width = 100, Height = 50 };
- label.Content = finder;
- label.VerticalContentAlignment = VerticalA-lignment.Top;
- label.HorizontalAlignment = HorizontalAlign-ment.Left;
- label.FontWeight = FontWeights.Bold;
- stackPanel.Children.Add(label);
- chanalstack[i] = finder;
- channalsListBox.Items.Add(stackPanel);
- i++;
- }
- }
- public static BitmapImage ToBitmapImage(Bitmap bit-map)
- {
- using (var memory = new MemoryStream())
- {
- bitmap.Save(memory, ImageFormat.Png);
- memory.Position = 0;
- var bitmapImage = new BitmapImage();
- bitmapImage.BeginInit();
- bitmapImage.StreamSource = memory;
- bitmapImage.CacheOption = BitmapCacheOp-tion.OnLoad;
- bitmapImage.EndInit();
- bitmapImage.Freeze();
- return bitmapImage;
- }
- }
- private static string[,] getRssData(string channel)
- {
- System.Net.WebRequest myRequest = Sys-tem.Net.WebRequest.Create(channel);
- System.Net.WebResponse myResponse = myRe-quest.GetResponse();
- System.IO.Stream rssStream = myRe-sponse.GetResponseStream();
- System.Xml.XmlDocument rssDoc = new Sys-tem.Xml.XmlDocument();
- rssDoc.Load(rssStream);
- System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
- String[,] tempRssData = new String[rssItems.Count, 4];
- for (int i = 0; i < rssItems.Count; i++)
- {
- System.Xml.XmlNode rssNode;
- rssNode = rssItems.Item(i).SelectSingleNode("title");
- if (rssNode != null)
- {
- tempRssData[i, 0] = rssNode.InnerText;
- }
- else
- {
- tempRssData[i, 0] = "";
- }
- rssNode = rssItems.Item(i).SelectSingleNode("description");
- if (rssNode != null)
- {
- tempRssData[i, 1] = rssNode.InnerText;
- }
- else
- {
- tempRssData[i, 1] = "";
- }
- rssNode = rssItems.Item(i).SelectSingleNode("link");
- if (rssNode != null)
- {
- tempRssData[i, 2] = rssNode.InnerText;
- }
- else
- {
- tempRssData[i, 2] = "";
- }
- rssNode = rssItems.Item(i).SelectSingleNode("enclosure");
- if (rssNode != null)
- {
- string rssNodel = rssNode.OuterXml.Substring(16);
- tempRssData[i, 3] = rssNodel.Substring(0, rssNodel.IndexOf('"'));
- }
- else
- {
- tempRssData[i, 3] = "";
- }
- }
- return tempRssData;
- }
- private void refreshButton_Click(object sender, RoutedEventArgs e)
- {
- //
- //
- rssData = getRssData(channelTextBox.Text);
- contentListBox.Items.Clear();
- for (int i = 0; i < rssData.GetLength(0); i++)
- {
- if (rssData[i, 0] != null)
- {
- StackPanel stackPanel = new StackPanel { Width = 520, Height = 200 };
- Label label = new Label { Width = 520, Height = 50 };
- label.Content = rssData[i, 0];
- label.VerticalContentAlignment = Verti-calAlignment.Top;
- label.HorizontalAlignment = Horizonta-lAlignment.Left;
- label.FontWeight = FontWeights.Bold;
- stackPanel.Children.Add(label);
- try
- {
- Bitmap Picture = new Bitmap(new WebClient().OpenRead(rssData[i, 3].ToString()));
- BitmapImage WorkImg = ToBit-mapImage(Picture);
- stackPanel.Children.Add(new Image
- {
- Source = WorkImg,
- Stretch = Stretch.Fill,
- Width = 120,
- Height = 80
- });
- }
- catch
- {
- //MessageBox.Show("Error loading the image");
- }
- TextBlock TB = new TextBlock { Width = 520, Height = 100 };
- TB.Text = rssData[i, 1];
- TB.TextWrapping = TextWrapping.Wrap;
- TB.TextAlignment = TextAlignment.Left;
- stackPanel.Children.Add(TB);
- contentListBox.Items.Add(stackPanel);
- }
- }
- }
- private void contentListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
- {
- Pro-cess.Start(rssData[contentListBox.SelectedIndex, 2]);
- }
- private void addchannalsButton_Click(object sender, RoutedEventArgs e)
- {
- AddWindow newWindow = new AddWindow(this, Chan-nals, chanalstack);
- newWindow.Show();
- }
- private void channalsListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
- {
- string keyfinder = cha-nalstack[channalsListBox.SelectedIndex];
- urlfinder = Channals[keyfinder];
- rssData = getRssData(urlfinder);
- contentListBox.Items.Clear();
- for (int i = 0; i < rssData.GetLength(0); i++)
- {
- if (rssData[i, 0] != null)
- {
- StackPanel stackPanel = new StackPanel { Width = 520, Height = 200 };
- Label label = new Label { Width = 520, Height = 50 };
- label.Content = rssData[i, 0];
- label.VerticalContentAlignment = Verti-calAlignment.Top;
- label.HorizontalAlignment = Horizonta-lAlignment.Left;
- label.FontWeight = FontWeights.Bold;
- stackPanel.Children.Add(label);
- try
- {
- Bitmap Picture = new Bitmap(new WebClient().OpenRead(rssData[i, 3].ToString()));
- BitmapImage WorkImg = ToBit-mapImage(Picture);
- stackPanel.Children.Add(new Image
- {
- Source = WorkImg,
- Stretch = Stretch.Fill,
- Width = 120,
- Height = 80
- });
- }
- catch
- {
- //MessageBox.Show("Error loading the image");
- }
- TextBlock TB = new TextBlock { Width = 520, Height = 100 };
- TB.Text = rssData[i, 1];
- TB.TextWrapping = TextWrapping.Wrap;
- TB.TextAlignment = TextAlignment.Left;
- stackPanel.Children.Add(TB);
- contentListBox.Items.Add(stackPanel);
- }
- }
- }
- private void deletechannalButton_Click(object send-er, RoutedEventArgs e)
- {
- string keyfinder = cha-nalstack[channalsListBox.SelectedIndex];
- if (Channals[keyfinder] == urlfinder)
- {
- contentListBox.Items.Clear();
- }
- Channals.Remove(keyfinder);
- channalsListBox.Items.Clear();
- int i = 0;
- foreach (string finder in Channals.Keys)
- {
- StackPanel stackPanel = new StackPanel { Width = 100, Height = 50 };
- Label label = new Label { Width = 100, Height = 50 };
- label.Content = finder;
- label.VerticalContentAlignment = VerticalA-lignment.Top;
- label.HorizontalAlignment = HorizontalAlign-ment.Left;
- label.FontWeight = FontWeights.Bold;
- stackPanel.Children.Add(label);
- chanalstack[i] = finder;
- channalsListBox.Items.Add(stackPanel);
- i++;
- }
- }
- private void Window_Closing(object sender, Sys-tem.ComponentModel.CancelEventArgs e)
- {
- var stringbuilder = new StringBuilder();
- foreach (var kv in Channals)
- {
- stringbuild-er.AppendLine($"{kv.Key}={kv.Value}");
- }
- var text = stringbuilder.ToString();
- if (text != null ^ text != "" ^ text != " ")
- {
- using (StreamWriter writer = new StreamWrit-er(ProgramPath + "\\" + "Chanals.txt", false, Sys-tem.Text.Encoding.Default))
- {
- writer.WriteLine(text);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement