源代码:C#实现简单的Tail

以下代码没有严格测试,能用,参数的地方有些错误,没有修正,自个看看
代码中打开文件如果要用OpenFileDialog需要首先设置main的线程模式,缺点是弹出的对话框居然跑到后面去了,每什么意思。,
我现在是将这个东西绑定到右键菜单了(嘻嘻,优化大师省事不少)
代码没有整理,有些乱,呵呵,草就的一个东西,就不苛求了
ContractedBlock.gif ExpandedBlockStart.gif 源代码
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.Text;
  4None.gifusing System.IO;
  5None.gifusing System.Threading;
  6None.gifusing System.Windows.Forms;
  7None.gif
  8None.gifnamespace Tail
  9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 10InBlock.gif    class Program
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        static ConsoleColor USAGE = ConsoleColor.Red;
 13InBlock.gif        static ConsoleColor P1 = ConsoleColor.Yellow;
 14InBlock.gif        static ConsoleColor P2 = ConsoleColor.White;
 15InBlock.gif        static bool m_block = false;
 16InBlock.gif        static bool m_exit = false;
 17InBlock.gif        private static void showFile(String strFileName)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            System.Console.Title = Application.ProductName+">" + strFileName;
 20InBlock.gif            long lfFileSize = -1;
 21InBlock.gif            int nCount = 0;
 22InBlock.gif            System.IO.FileStream fs = null;
 23InBlock.gif            if (File.Exists(strFileName) == false)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 25InBlock.gif                System.Console.ForegroundColor = USAGE;
 26InBlock.gif                System.Console.WriteLine("文件 "+strFileName+"没有找到!");
 27InBlock.gif                m_block = true;
 28ExpandedSubBlockEnd.gif            }

 29InBlock.gif            try
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 31InBlock.gif                while (true)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 33InBlock.gif                    BlockOperation(ref strFileName, ref lfFileSize, ref nCount);
 34InBlock.gif                    if (m_exit)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 36InBlock.gif                        break;
 37ExpandedSubBlockEnd.gif                    }

 38InBlock.gif                    if (m_block)
 39ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 40InBlock.gif                        Thread.Sleep(200);
 41InBlock.gif                        continue;
 42ExpandedSubBlockEnd.gif                    }

 43InBlock.gif
 44InBlock.gif                    FileInfo f = new FileInfo(strFileName);
 45InBlock.gif                    if (lfFileSize < 0)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 47InBlock.gif                        lfFileSize = Math.Max(0, f.Length - 255);
 48ExpandedSubBlockEnd.gif                    }

 49InBlock.gif                    if (false == (lfFileSize == 0 || lfFileSize != f.Length))
 50ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 51InBlock.gif                        Thread.Sleep(200);
 52InBlock.gif                        continue;
 53ExpandedSubBlockEnd.gif                    }

 54InBlock.gif                    if (f.Length < lfFileSize)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 56InBlock.gif                        System.Console.ForegroundColor = USAGE;
 57InBlock.gif                        System.Console.WriteLine();
 58InBlock.gif                        System.Console.WriteLine("--------------文件变更,需要重新读取文件----------------------------");
 59InBlock.gif                        lfFileSize = Math.Max(0, f.Length - 255);
 60ExpandedSubBlockEnd.gif                    }

 61InBlock.gif                    fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
 62InBlock.gif                    fs.Seek(lfFileSize, SeekOrigin.Begin);
 63InBlock.gif                    byte[] bs = new byte[4096];
 64InBlock.gif                    int n = 0;
 65InBlock.gif                    bool bBegin = true;
 66InBlock.gif                    while ((n = fs.Read(bs, 04096)) > 0)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 68InBlock.gif                        if (bBegin)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 70InBlock.gif                            nCount++;
 71InBlock.gif                            System.Console.ForegroundColor = nCount % 2 == 0 ? P1 : P2;
 72ExpandedSubBlockEnd.gif                        }

 73InBlock.gif                        System.Console.Write(System.Text.Encoding.Default.GetString(bs, 0, n));
 74InBlock.gif                        bBegin = false;
 75ExpandedSubBlockEnd.gif                    }

 76InBlock.gif                    lfFileSize = f.Length;
 77InBlock.gif                    Thread.Sleep(500);
 78InBlock.gif
 79ExpandedSubBlockEnd.gif                }

 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif            catch (Exception excep)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83ExpandedSubBlockEnd.gif            }

 84InBlock.gif            finally
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 86InBlock.gif                try
 87ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 88InBlock.gif                    if (null != fs)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 90InBlock.gif                        fs.Close();
 91ExpandedSubBlockEnd.gif                    }

 92ExpandedSubBlockEnd.gif                }

 93InBlock.gif                catch
 94ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 95ExpandedSubBlockEnd.gif                }

 96ExpandedSubBlockEnd.gif            }

 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif
 99InBlock.gif        private static void BlockOperation(ref String strFileName, ref long lfFileSize, ref int nCount)
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            if (m_block)
102ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
103InBlock.gif                m_block = false;
104InBlock.gif                System.Console.ForegroundColor = USAGE;
105InBlock.gif                System.Console.WriteLine(Application.ProductName + "[" + Application.ProductVersion + "]" + Application.CompanyName);
106InBlock.gif                System.Console.Write("输入命令[帮助输入?或者help]:");
107InBlock.gif                String str = System.Console.ReadLine();
108InBlock.gif                if (str != null && str.Trim().Length > 0)
109ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
110InBlock.gif                    str = str.Trim().ToLower();
111InBlock.gif                    if (str.Equals("clear"))
112ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
113InBlock.gif                        System.Console.Clear();
114InBlock.gif                        m_block = true;
115ExpandedSubBlockEnd.gif                    }

116InBlock.gif                    if (str.Equals("continue"))
117ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
118InBlock.gif                        m_block = false;
119ExpandedSubBlockEnd.gif                    }

120InBlock.gif                    else if (str.Equals("exit"|| str.Equals("bye"))
121ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
122InBlock.gif                        m_exit = true;
123ExpandedSubBlockEnd.gif                    }

124InBlock.gif                    else if (str.Equals("?"|| str.Equals("help"))
125ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
126InBlock.gif                        System.Console.WriteLine("continue:继续[回车]");
127InBlock.gif                        System.Console.WriteLine("clear:清除当前的屏幕");
128InBlock.gif                        System.Console.WriteLine("exit:退出");
129InBlock.gif                        System.Console.WriteLine("open:打开文件 open filename");
130InBlock.gif                        m_block = true;
131ExpandedSubBlockEnd.gif                    }

132InBlock.gif                    else if (str.StartsWith("open"))
133ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
134InBlock.gif                        if (str.StartsWith("open "))
135ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
136InBlock.gif                            strFileName = str.Substring(5);
137InBlock.gif                            if (File.Exists(strFileName) == false)
138ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
139InBlock.gif                                System.Console.ForegroundColor = USAGE;
140InBlock.gif                                System.Console.WriteLine("文件 " + strFileName + "没有找到!");
141InBlock.gif                                m_block = true;
142ExpandedSubBlockEnd.gif                            }

143InBlock.gif                            nCount = 0;
144InBlock.gif                            System.Console.Title = Application.ProductName+">" + strFileName;
145InBlock.gif                            lfFileSize = -1;
146ExpandedSubBlockEnd.gif                        }

147InBlock.gif                        else
148ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
149InBlock.gif                            System.Console.ForegroundColor = USAGE;
150InBlock.gif                            System.Console.WriteLine("命令open的格式不正确!");
151InBlock.gif                            m_block = true;
152ExpandedSubBlockEnd.gif                        }

153ExpandedSubBlockEnd.gif                    }

154InBlock.gif                    else
155ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
156InBlock.gif                        System.Console.ForegroundColor = USAGE;
157InBlock.gif                        System.Console.WriteLine("命令:"+str+"不能被识别!");
158InBlock.gif                        m_block = true;
159ExpandedSubBlockEnd.gif                    }

160ExpandedSubBlockEnd.gif                }
                
161ExpandedSubBlockEnd.gif            }

162ExpandedSubBlockEnd.gif        }

163InBlock.gif        static void Main(string[] args)
164ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
165InBlock.gif            System.Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
166InBlock.gif            System.Console.BufferWidth = 2048;
167InBlock.gif            System.Console.BufferHeight = 1024;
168InBlock.gif            if (args.Length <= 0)
169ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
170InBlock.gif                //if (GetInput(null) == false)
171ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
172InBlock.gif                    return;
173ExpandedSubBlockEnd.gif                }

174ExpandedSubBlockEnd.gif            }

175InBlock.gif            else
176ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
177InBlock.gif                //usage();
178InBlock.gif                showFile(args[0]);
179ExpandedSubBlockEnd.gif            }

180ExpandedSubBlockEnd.gif        }

181InBlock.gif
182InBlock.gif        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
183ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
184InBlock.gif            e.Cancel = true;
185InBlock.gif            m_block = true;
186ExpandedSubBlockEnd.gif        }

187ExpandedSubBlockEnd.gif    }

188ExpandedBlockEnd.gif}

189None.gif

转载于:https://www.cnblogs.com/JeasonZhao/archive/2007/06/29/799699.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值