如何查找ms word文档是只读还是读写 [英] how to find whether a ms word document is read only or read write

查看:95
本文介绍了如何查找ms word文档是只读还是读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在c#中打开一个ms word文档.我需要提前查找目标文档是否已在使用中(即查找它是只读还是读写).任何人都请帮助我.谢谢.

i am opening a ms word document in c#.i need to find in advance if the target document is already in use.(i.e to find is it read only or read write). anyone please help me. thanks.

推荐答案

您可以尝试以下文件属性:
You could try the following for the file attribute:
FileInfo fileInfo = new FileInfo(@"path\to\file"); 
if (fileInfo.IsReadOnly) 
{ 
    // do something 
} 



或是否要检查文件是否正在使用中:



or if you want to check if the file is in use:

protected virtual bool IsFileLocked(FileInfo file) 
{ 
    FileStream stream = null; 
 
    try 
    { 
        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); 
    } 
    catch (IOException) 
    { 
        //the file is unavailable because it is: 
        //still being written to 
        //or being processed by another thread 
        //or does not exist (has already been processed) 
        return true; 
    } 
    finally 
    { 
        if (stream != null) 
            stream.Close(); 
    } 
 
    //file is not locked 
    return false; 
} 


这篇关于如何查找ms word文档是只读还是读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆