在线等,解决马上解帖。

解决方案 »

  1.   

    尝试去改名,改成功了,说明没被使用,再改回去就可以了~~~~~~~********************************************************************
    *TryMyBestToKnowSomethingAboutEverythingAndEverythingAboutSomething! 
    ********************************************************************
      

  2.   

    如果只是侦测文件的打开,而不需要做到文件锁之类的功能的话,FileSystemWatcher可以检测文件的LastAccess time当该值改变时激发Changed事件,这样就可以达到效果了。
    但是,在我的机器上有个问题,以前一直没注意到:
    在C盘下的文件的LastAccess Time可以精确到秒但是其他盘符下的文件的LastAccess Time只能精确到Day,这样就无法监控读取了。这个问题有哪位能解释一下??
      

  3.   

    先谢谢大家热心参与
    我只是想检测worddocument类的文件是否打开,如果打开我就关闭它,现在感觉比较棘手,虽然g961681可以,但是有没有更好的了?
      

  4.   

    在try{打开}catch(Exception e){关闭} 如果是打开的在次打开肯定会有异常,用catch()捕捉到异常后关掉它,应该就可以了。
      

  5.   

    try
    {
       File.Move(...); //to another filename
    }
    catch(UnauthorizedAccessException e)
    {
       Console.Writeline("No permission"); //no permisson or file is locked.
    }
    final
    {
       File.Move(..);//rollback file name
    }
      

  6.   

    另一个方法,用FileStream.CanWrite来判断,代码如下using System;
    using System.IO;
    using System.Text;class Test 
    {
        
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";        // Ensure that the file is readonly.
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);        //Create the file.
            using (FileStream fs = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)) 
            {
                if (fs.CanWrite) 
                {
                    Console.WriteLine("The stream for file {0} is writable.", path);
                } 
                else 
                {
                    Console.WriteLine("The stream for file {0} is not writable.", path);
                }
            }
        }
    }
      

  7.   

    在给一段参考代码
    using System;
    using System.IO;
     
    class TestRW 
    {
        public static void Main(String[] args)
        {
            FileStream fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Write);
            if (fs.CanRead && fs.CanWrite)
            {
                Console.WriteLine("MyFile.txt can be both written to and read from.");
            }
            else if (fs.CanWrite)
            {
                Console.WriteLine("MyFile.txt is writable.");
            }
        }
    }
      

  8.   

    现在关键就是不知道怎么关闭文件了,我的想法是根据文件名找到相应的进程,然后Kill掉进程,再把硬盘上响应的~$......文件删掉。不知道大家意见如何
      

  9.   

    word 是独占方式打开文件的,jamesfay(James Fay) 的方法就可以*Johnny_de(天才) (
    现在关键就是不知道怎么关闭文件了,我的想法是根据文件名找到相应的进程,然后Kill掉进程,再把硬盘上响应的~$......文件删掉。不知道大家意见如何*根据文件名找到相应的进程 有些安装程序可以不知道怎么做的]*然后Kill掉进程
    想法很好,不过在windows 2000 以后的系统
    是不可以随便关闭,非程序调用的其他程序的
    比如如果word 是你调用的可以写代码关闭
    如果是人双击打开的,需要调用几个API提升程序得到 debug 权限令牌 才可以关闭其他程序
      

  10.   

    好像只能使用文件监视器,对方打开文档使用ShareRead和ShareWrite模式就不能使用CanRead和 CanWrite判断文件是否打开了
    如LZ想要客户机上不能打开Word文件修改Word文档的注册表Command项就可以了啊
      

  11.   

    我不是不想让人家打开Word,只是在我处理相应的word文件时需要回收资源。
      

  12.   

    不要删除$文件,会出问题的如果要关闭doc的话,正确的做法是,用word automation,建立一个word applicatino打开你的目标文档,然后再关闭doc并释放word object。这篇文章关于word automation(how to create a new doc),应该能借鉴一下
    http://support.microsoft.com/default.aspx?scid=kb;en-us;316384
      

  13.   

    大致可以这样1. 查看FileStream.CanWrite发现不能写
    2. 用automation,获取一个存在的word的instance,关闭文件,关闭winword(枚举每个实例)获取正在运行的实例可以参考一下做法,我没有试过,我只用过access
    Access.Application oAccess = null;
    oAccess = (Access.Application)Marshal.GetActiveObject("Access.Application");
      

  14.   

    FileStream fs = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read)
    当文件处于打开状态时就会抛出异常的.
    我想问以后怎样把文件以worddocument的方式关闭?
      

  15.   

    哎,都说得这么清楚来还不明白?
    给你段代码算了,我调试过的,好用的。
    这段代码的作用是找到所有打开的winword程序(不是进程),然后关闭掉。
    你参考下吧
    using System;
    using System.Runtime.InteropServices;
    using Word; //Add Com Reference: Microsoft Word 10.0 Object Librarynamespace CloseDocSample
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //Define parameter value for winword
    object oMissing = System.Reflection.Missing.Value;

    //Find the exist winword application
    Word.Application winObj = (Word.Application)Marshal.GetActiveObject("Word.Application"); //Close the application
    winObj.Quit(ref oMissing, ref oMissing, ref oMissing); //Release object
    System.Runtime.InteropServices.Marshal.ReleaseComObject(winObj);
    winObj = null;
    }
    }
    }
    要更多的参考可依照support.microsoft.com 关键字:automate word office c#