源代码:C#实现简单的Tail
以下代码没有严格测试,能用,参数的地方有些错误,没有修正,自个看看
代码中打开文件如果要用OpenFileDialog需要首先设置main的线程模式,缺点是弹出的对话框居然跑到后面去了,每什么意思。,
我现在是将这个东西绑定到右键菜单了(嘻嘻,优化大师省事不少)
代码没有整理,有些乱,呵呵,草就的一个东西,就不苛求了
代码中打开文件如果要用OpenFileDialog需要首先设置main的线程模式,缺点是弹出的对话框居然跑到后面去了,每什么意思。,
我现在是将这个东西绑定到右键菜单了(嘻嘻,优化大师省事不少)
代码没有整理,有些乱,呵呵,草就的一个东西,就不苛求了
源代码
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.Threading;
6using System.Windows.Forms;
7
8namespace Tail
9{
10 class Program
11 {
12 static ConsoleColor USAGE = ConsoleColor.Red;
13 static ConsoleColor P1 = ConsoleColor.Yellow;
14 static ConsoleColor P2 = ConsoleColor.White;
15 static bool m_block = false;
16 static bool m_exit = false;
17 private static void showFile(String strFileName)
18 {
19 System.Console.Title = Application.ProductName+">" + strFileName;
20 long lfFileSize = -1;
21 int nCount = 0;
22 System.IO.FileStream fs = null;
23 if (File.Exists(strFileName) == false)
24 {
25 System.Console.ForegroundColor = USAGE;
26 System.Console.WriteLine("文件 "+strFileName+"没有找到!");
27 m_block = true;
28 }
29 try
30 {
31 while (true)
32 {
33 BlockOperation(ref strFileName, ref lfFileSize, ref nCount);
34 if (m_exit)
35 {
36 break;
37 }
38 if (m_block)
39 {
40 Thread.Sleep(200);
41 continue;
42 }
43
44 FileInfo f = new FileInfo(strFileName);
45 if (lfFileSize < 0)
46 {
47 lfFileSize = Math.Max(0, f.Length - 255);
48 }
49 if (false == (lfFileSize == 0 || lfFileSize != f.Length))
50 {
51 Thread.Sleep(200);
52 continue;
53 }
54 if (f.Length < lfFileSize)
55 {
56 System.Console.ForegroundColor = USAGE;
57 System.Console.WriteLine();
58 System.Console.WriteLine("--------------文件变更,需要重新读取文件----------------------------");
59 lfFileSize = Math.Max(0, f.Length - 255);
60 }
61 fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
62 fs.Seek(lfFileSize, SeekOrigin.Begin);
63 byte[] bs = new byte[4096];
64 int n = 0;
65 bool bBegin = true;
66 while ((n = fs.Read(bs, 0, 4096)) > 0)
67 {
68 if (bBegin)
69 {
70 nCount++;
71 System.Console.ForegroundColor = nCount % 2 == 0 ? P1 : P2;
72 }
73 System.Console.Write(System.Text.Encoding.Default.GetString(bs, 0, n));
74 bBegin = false;
75 }
76 lfFileSize = f.Length;
77 Thread.Sleep(500);
78
79 }
80 }
81 catch (Exception excep)
82 {
83 }
84 finally
85 {
86 try
87 {
88 if (null != fs)
89 {
90 fs.Close();
91 }
92 }
93 catch
94 {
95 }
96 }
97 }
98
99 private static void BlockOperation(ref String strFileName, ref long lfFileSize, ref int nCount)
100 {
101 if (m_block)
102 {
103 m_block = false;
104 System.Console.ForegroundColor = USAGE;
105 System.Console.WriteLine(Application.ProductName + "[" + Application.ProductVersion + "]" + Application.CompanyName);
106 System.Console.Write("输入命令[帮助输入?或者help]:");
107 String str = System.Console.ReadLine();
108 if (str != null && str.Trim().Length > 0)
109 {
110 str = str.Trim().ToLower();
111 if (str.Equals("clear"))
112 {
113 System.Console.Clear();
114 m_block = true;
115 }
116 if (str.Equals("continue"))
117 {
118 m_block = false;
119 }
120 else if (str.Equals("exit") || str.Equals("bye"))
121 {
122 m_exit = true;
123 }
124 else if (str.Equals("?") || str.Equals("help"))
125 {
126 System.Console.WriteLine("continue:继续[回车]");
127 System.Console.WriteLine("clear:清除当前的屏幕");
128 System.Console.WriteLine("exit:退出");
129 System.Console.WriteLine("open:打开文件 open filename");
130 m_block = true;
131 }
132 else if (str.StartsWith("open"))
133 {
134 if (str.StartsWith("open "))
135 {
136 strFileName = str.Substring(5);
137 if (File.Exists(strFileName) == false)
138 {
139 System.Console.ForegroundColor = USAGE;
140 System.Console.WriteLine("文件 " + strFileName + "没有找到!");
141 m_block = true;
142 }
143 nCount = 0;
144 System.Console.Title = Application.ProductName+">" + strFileName;
145 lfFileSize = -1;
146 }
147 else
148 {
149 System.Console.ForegroundColor = USAGE;
150 System.Console.WriteLine("命令open的格式不正确!");
151 m_block = true;
152 }
153 }
154 else
155 {
156 System.Console.ForegroundColor = USAGE;
157 System.Console.WriteLine("命令:"+str+"不能被识别!");
158 m_block = true;
159 }
160 }
161 }
162 }
163 static void Main(string[] args)
164 {
165 System.Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
166 System.Console.BufferWidth = 2048;
167 System.Console.BufferHeight = 1024;
168 if (args.Length <= 0)
169 {
170 //if (GetInput(null) == false)
171 {
172 return;
173 }
174 }
175 else
176 {
177 //usage();
178 showFile(args[0]);
179 }
180 }
181
182 static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
183 {
184 e.Cancel = true;
185 m_block = true;
186 }
187 }
188}
189
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.Threading;
6using System.Windows.Forms;
7
8namespace Tail
9{
10 class Program
11 {
12 static ConsoleColor USAGE = ConsoleColor.Red;
13 static ConsoleColor P1 = ConsoleColor.Yellow;
14 static ConsoleColor P2 = ConsoleColor.White;
15 static bool m_block = false;
16 static bool m_exit = false;
17 private static void showFile(String strFileName)
18 {
19 System.Console.Title = Application.ProductName+">" + strFileName;
20 long lfFileSize = -1;
21 int nCount = 0;
22 System.IO.FileStream fs = null;
23 if (File.Exists(strFileName) == false)
24 {
25 System.Console.ForegroundColor = USAGE;
26 System.Console.WriteLine("文件 "+strFileName+"没有找到!");
27 m_block = true;
28 }
29 try
30 {
31 while (true)
32 {
33 BlockOperation(ref strFileName, ref lfFileSize, ref nCount);
34 if (m_exit)
35 {
36 break;
37 }
38 if (m_block)
39 {
40 Thread.Sleep(200);
41 continue;
42 }
43
44 FileInfo f = new FileInfo(strFileName);
45 if (lfFileSize < 0)
46 {
47 lfFileSize = Math.Max(0, f.Length - 255);
48 }
49 if (false == (lfFileSize == 0 || lfFileSize != f.Length))
50 {
51 Thread.Sleep(200);
52 continue;
53 }
54 if (f.Length < lfFileSize)
55 {
56 System.Console.ForegroundColor = USAGE;
57 System.Console.WriteLine();
58 System.Console.WriteLine("--------------文件变更,需要重新读取文件----------------------------");
59 lfFileSize = Math.Max(0, f.Length - 255);
60 }
61 fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
62 fs.Seek(lfFileSize, SeekOrigin.Begin);
63 byte[] bs = new byte[4096];
64 int n = 0;
65 bool bBegin = true;
66 while ((n = fs.Read(bs, 0, 4096)) > 0)
67 {
68 if (bBegin)
69 {
70 nCount++;
71 System.Console.ForegroundColor = nCount % 2 == 0 ? P1 : P2;
72 }
73 System.Console.Write(System.Text.Encoding.Default.GetString(bs, 0, n));
74 bBegin = false;
75 }
76 lfFileSize = f.Length;
77 Thread.Sleep(500);
78
79 }
80 }
81 catch (Exception excep)
82 {
83 }
84 finally
85 {
86 try
87 {
88 if (null != fs)
89 {
90 fs.Close();
91 }
92 }
93 catch
94 {
95 }
96 }
97 }
98
99 private static void BlockOperation(ref String strFileName, ref long lfFileSize, ref int nCount)
100 {
101 if (m_block)
102 {
103 m_block = false;
104 System.Console.ForegroundColor = USAGE;
105 System.Console.WriteLine(Application.ProductName + "[" + Application.ProductVersion + "]" + Application.CompanyName);
106 System.Console.Write("输入命令[帮助输入?或者help]:");
107 String str = System.Console.ReadLine();
108 if (str != null && str.Trim().Length > 0)
109 {
110 str = str.Trim().ToLower();
111 if (str.Equals("clear"))
112 {
113 System.Console.Clear();
114 m_block = true;
115 }
116 if (str.Equals("continue"))
117 {
118 m_block = false;
119 }
120 else if (str.Equals("exit") || str.Equals("bye"))
121 {
122 m_exit = true;
123 }
124 else if (str.Equals("?") || str.Equals("help"))
125 {
126 System.Console.WriteLine("continue:继续[回车]");
127 System.Console.WriteLine("clear:清除当前的屏幕");
128 System.Console.WriteLine("exit:退出");
129 System.Console.WriteLine("open:打开文件 open filename");
130 m_block = true;
131 }
132 else if (str.StartsWith("open"))
133 {
134 if (str.StartsWith("open "))
135 {
136 strFileName = str.Substring(5);
137 if (File.Exists(strFileName) == false)
138 {
139 System.Console.ForegroundColor = USAGE;
140 System.Console.WriteLine("文件 " + strFileName + "没有找到!");
141 m_block = true;
142 }
143 nCount = 0;
144 System.Console.Title = Application.ProductName+">" + strFileName;
145 lfFileSize = -1;
146 }
147 else
148 {
149 System.Console.ForegroundColor = USAGE;
150 System.Console.WriteLine("命令open的格式不正确!");
151 m_block = true;
152 }
153 }
154 else
155 {
156 System.Console.ForegroundColor = USAGE;
157 System.Console.WriteLine("命令:"+str+"不能被识别!");
158 m_block = true;
159 }
160 }
161 }
162 }
163 static void Main(string[] args)
164 {
165 System.Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
166 System.Console.BufferWidth = 2048;
167 System.Console.BufferHeight = 1024;
168 if (args.Length <= 0)
169 {
170 //if (GetInput(null) == false)
171 {
172 return;
173 }
174 }
175 else
176 {
177 //usage();
178 showFile(args[0]);
179 }
180 }
181
182 static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
183 {
184 e.Cancel = true;
185 m_block = true;
186 }
187 }
188}
189