開発環境 Microsoft Visual C# 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Windows フォーム アプリケーション
プロジェクト名 Jien2ch

プロジェクト


プロジェクトの保存
[ソリューションのディレクトリを作成]にチェックを付けておくと、
ソリューションに新しいプロジェクトを追加する時にプロジェクトの階層が整う。

フォーム


名前:Form1
プロパティ Text:Jien2ch Size:800, 600

コンテナー/SplitContainerをForm1上にドロップ。
名前:splitContainer1
プロパティ FixedPanel:Panel1

コモン コントロール/TreeViewをPanel1上にドロップ。
名前:treeView1
プロパティ Dock:Fill

コモン コントロール/TextBoxをPanel2上にドロップ。
名前:textBox1
プロパティ Dock:Fill MultiLine:True ScrollBars:Vertical

クラスライブラリ


ソリューションに新しいプロジェクトを追加。
テンプレート:クラス ライブラリ
名前:Lib2ch
Class1.csは削除。
Lib2chに以下の新しいフォルダーを追加。

Data
IO
Model

各種クラスを追加。

+Lib2ch
 +Data
  +Data.cs
 +IO
  +IO.cs
 +Model
  +BbsMenu.cs

Jien2chプロジェクトの参照設定にプロジェクトLib2chを追加。

参考

Form1.cs
using System.Collections.Generic;
using System.Windows.Forms;
using Lib2ch.Data;
using Lib2ch.Model;
 
namespace Jien2ch
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
 
			#region 板一覧
			BbsMenu bbsMenu = new BbsMenu();
			List<BbsMenuItem> bbsMenuList = bbsMenu.GetMenuList();
 
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (BbsMenuItem item in bbsMenuList)
			{
				TreeNode childNode = new TreeNode();
				childNode.Text = item.BbsName;
				childNode.Tag = item;
				// 親ノード検索
				TreeNode[] node = treeView1.Nodes.Find(item.Category, false);
				if (node.Length == 0)
				{
					TreeNode parent = new TreeNode();
					parent.Name = item.Category;	// 検索用
					parent.Text = item.Category;	// 表示用
					parent.Nodes.Add(childNode);
					treeView1.Nodes.Add(parent);
				}
				else
				{
					node[0].Nodes.Add(childNode);
				}
				sb.Append(item.Category + item.BbsName + item.Url + "\r\n");
			}
			textBox1.Text = sb.ToString();
			#endregion
		}
	}
}
 

Data.cs
namespace Lib2ch.Data
{
	// ファイル結果
	class FileResult
	{
		public string[] Line;	// 行リスト
	}
 
	// 板一覧アイテム
	public class BbsMenuItem
	{
		public string Category;	// カテゴリ
		public string BbsName;	// 板名
		public string Url;	// URL
	}
}
 

IO.cs
using System.IO;
using System.Net;
using System.Text;
using Lib2ch.Data;
 
namespace Lib2ch.IO
{
	class FileManager
	{
		public FileResult StandardCheck(string dir, string host, string file)
		{
			FileResult fileResult = LocalReader(dir + file);
			if (fileResult == null)
			{
				// サーバーからダウンロード
				Download(dir, host, file);
				fileResult = LocalReader(dir + file);
			}
			return fileResult;
		}
 
		FileResult LocalReader(string path)
		{
			try
			{
				FileResult fileResult = new FileResult();
				fileResult.Line = File.ReadAllLines(path, Encoding.Default);
				return fileResult;
			}
			catch
			{
				return null;
			}
		}
 
		bool Download(string dir, string host, string file)
		{
			if (!Directory.Exists(dir))
			{
				Directory.CreateDirectory(dir);
			}
			try
			{
				WebClient wc = new WebClient();
				wc.DownloadFile(host + file, dir + file);
				return true;
			}
			catch
			{
				return false;
			}
		}
	}
}
 

BbsMenu.cs
using System.Collections.Generic;
using Lib2ch.Data;
using Lib2ch.IO;
 
namespace Lib2ch.Model
{
	public class BbsMenu
	{
		const string _CateStart = "<BR><BR><B>";
		const string _CateEnd = "</B>";
		const string _BbsStart = "<A HREF=";
		const string _UrlEnd1 = " TARGET=_blank>";
		const string _UrlEnd2 = ">";
		const string _BbsEnd = "</A>";
 
		FileManager _File = new FileManager();
 
		public List<BbsMenuItem> GetMenuList()
		{
			// ファイルの有無
			FileResult fileResult = _File.StandardCheck(
				"./Log/", "http://menu.2ch.net/", "bbsmenu.html");
			if (fileResult == null) return null;
 
			// 解析処理
			return BbsMenuParsing(fileResult.Line);
		}
 
		List<BbsMenuItem> BbsMenuParsing(string[] data)
		{
			List<BbsMenuItem> list = new List<BbsMenuItem>();
			string category = null;
			foreach (string line in data)
			{
				int index1 = line.IndexOf(_CateStart);
				if (index1 != -1)
				{
					int index2 = line.IndexOf(_CateEnd);
					if (index2 != -1)
					{
						int start = index1 + _CateStart.Length;
						category = line.Substring(start, index2 - start);
					}
				}
				if (category == null) continue;
				BbsMenuItem item = BbsMenuFindLine(line, _BbsStart, _UrlEnd1, _BbsEnd);
				if (item == null)
				{
					item = BbsMenuFindLine(line, _BbsStart, _UrlEnd2, _BbsEnd);
				}
				if (item != null)
				{
					item.Category = category;
					list.Add(item);
				}
			}
			return list;
		}
 
		BbsMenuItem BbsMenuFindLine(string line, string word1, string word2, string word3)
		{
			int index1 = line.IndexOf(word1);
			if (index1 == -1) return null;
			int start1 = index1 + word1.Length;
 
			int index2 = line.IndexOf(word2, start1);
			if (index2 == -1) return null;
			int start2 = index2 + word2.Length;
 
			int index3 = line.IndexOf(word3, start2);
			if (index3 == -1) return null;
 
			BbsMenuItem item = new BbsMenuItem();
			item.Url = line.Substring(start1, index2 - start1);
			item.BbsName = line.Substring(start2, index3 - start2);
			return item;
		}
	}
}
 
最終更新:2012年11月17日 19:56