日期:2014-05-17  浏览次数:20817 次

C#程序关于文件占用无法访问问题求解。。。
这个代码是一个鼠标键盘录制回放器的一部分代码,我简单说下。。。点record开始记录,record按钮文字变成stop. 点stop以后再点play可以正常播放,但在点了record没点stop时候点play会出错,OperationLog.txt被其它进程占用,不能访问。 不知道在哪关闭流,也不想用抛出错误的方法。。。求解.太穷 没多少分。。
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace UltroMouse
{
    
    public partial class FrmMain : Form
    {
        #region 私有常量

        private readonly int m_ScreenWidth = 1024;

        private readonly int m_ScreenHeight = 768;

        #endregion 私有常量

        #region 私有变量

        /// <summary>
        /// 屏幕捕获点X坐标
        /// </summary>
        private int m_CaptureX = 0;

        /// <summary>
        /// 屏幕捕获点Y坐标
        /// </summary>
        private int m_CaptureY = 0;

        /// <summary>
        /// 锁定对象,用于加锁
        /// </summary>
        private object m_LockObj = new object();

        /// <summary>
        /// 开始记录坐标
        /// </summary>
        private bool m_StartRecordPos = false;

        /// <summary>
        /// 时间cnt
        /// </summary>
        private UInt32 m_dwTimeCnt = 0;//100ms every Cnt.

        //add some variable here.
        private FileInfo f = new FileInfo("OperationLog.txt");
        private StreamWriter sw=null;
        private StreamReader sr=null;
        private bool m_Recording = false;
        private bool m_RequestShowForm = false;
        private bool m_StopExcuteRecord = false;

        /// <summary>
        /// 钩子管理实例
        /// </summary>
        private SKHook m_HookMain = new SKHook();

        #endregion 私有变量

        #region 私有方法


        /// <summary>
        ///    
        /// </summary>
        /// <param name="mousex"></param>
        /// <param name="mousey"></param>
        private void RecordCaptureXY(int code,int mousex, int mousey)
        {
            //label1.Text = "X=" + mousex.ToString();
            //label2.Text = "Y=" + mousey.ToString();

            //if record now, save the records to the log file.
            if(m_Recording&&Check_RecordMouse.Checked)
                sw.WriteLine("M,{0},{1},{2},{3}", m_dwTimeCnt * 100,code, mousex, mousey);
        }

        /// <summary>
        ///    
        /// </summary>
        /// <param name="mousex"></param>
        /// <param name="mousey"></param>
        private void RecordKeyCode(KeyEventArgs e,WM_KEYBOARD keystatus)
        {
            //if record now, save the records to the log file.
            if (m_Recording && Check_RecordKeyboard.Checked)
            {
                sw.WriteLine("K,{0},{1},{2}", m_dwTimeCnt * 100, (int)keystatus,(int)e.KeyCode);
            }
        }

        #endregion 私有方法

        #region 构造函数

        /// <summary>
        /// 主窗口
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();

            this.m_ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
            this.m_ScreenHeight = Screen.PrimaryScreen.Bounds.Height;

            this.m_HookMain.OnKeyDown += new KeyEventHandler(HookMain_OnKeyDown);
            this.m_HookMain.OnKeyUp += new KeyEventHandler(HookMain_OnKeyUp);
            this.m_HookMain.OnMouseUpdate += new MouseUpdateEventHandler(HookMain_OnMouseUpdate);
        }

        #endregion 构造函数

        #region 控件事件
   

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(obje