怎样知道一个文件是否正在被其他程序打开,并且正在被改写?

gulingfeng 2003-08-22 04:16:49
如题
...全文
138 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
gulingfeng 2003-08-25
  • 打赏
  • 举报
回复
恩,看来是不能完全解决了
不过还是很有收获,多谢各位!
wqbmercury 2003-08-25
  • 打赏
  • 举报
回复
哦!对了!楼上的这样子一说,我也了解了~
象画图程序,记事本打开文件用的就是楼上说的那样是离线方式(就是,打后文件,把文件读入后就关闭了文件了)后面是对内存中的数据操作
而word之类的是在线方式,也就是打开文件后,并没有关闭文件
所以用独占方式来判断时只能对用在线方式打开的能判别断!
HOHO,我也要多谢楼上的ok1234567了
ok1234567 2003-08-25
  • 打赏
  • 举报
回复
一个文件被其它程序使用,有两种方式:在线和离线
在线方式下:可以判断
离线方式下:不能判断
所以你的问题不能全部解决:)
因为程序打开文件,读取数据,然后关闭文件,是不会在文件中留下任何记录的
wqbmercury 2003-08-25
  • 打赏
  • 举报
回复
你可以看看这个贴子,我也做过测试用贴子中我说的那总方式
就像 guoxiny(狼) 说的
"看来并不是所有的文件都可以用独占打开的方式来判断。
象画图程序、记事本等打开的文件仍然可以独占打开,大家还知不知道其他的方法呢?"
后面, 我又试了好多方式都不行

http://expert.csdn.net/Expert/topic/1875/1875434.xml?temp=.7256281
gulingfeng 2003-08-25
  • 打赏
  • 举报
回复
@_@

为什么要再打开一次?没明白~~
不通过打开文件能不能知道别人有没有在访问?
zhouyong0371 2003-08-25
  • 打赏
  • 举报
回复
你只要通过代码实现保证文件一定是存在的就可以了。很好实现的。
zhouyong0371 2003-08-25
  • 打赏
  • 举报
回复
你再打开一次,并判断返回值,如果返回失败,证明已经打开了。

例如:
CString uint;

BOOL Readcfg(CString &uint)
{
CStdioFile cfile;
CString str;

if(!cfile.Open("C:\\Roy.txt",CFile::modeRead)) return FALSE;
if(!cfile.Open("C:\\Roy.txt",CFile::modeRead)) return FALSE;

BOOL bReturn = cfile.ReadString(uint); //这个地方要这样

return bReturn;

}

void CCStudioFileDlg::OnCstudiofile()
{
BOOL bReturn = Readcfg(uint);

if (bReturn ==TRUE)
{
AfxMessageBox(uint);
}
else
{
AfxMessageBox("have Opened");
}
}

gulingfeng 2003-08-25
  • 打赏
  • 举报
回复
非常感谢,不过这是监控目录下文件的变化,
我是想看一个已经存在的文件,我访问这个文件的时候,
这个文件有没有其他人也在访问。怎样知道呢?
:)
碍踢工匠 2003-08-22
  • 打赏
  • 举报
回复
Monitoring Changes in a Directory or Directory Tree
The following example monitors the directory tree starting at C:\ for directory name changes. It also monitors the C:\WINDOWS directory for file name changes.

The example uses the FindFirstChangeNotification function to create two notification handles and the WaitForMultipleObjects function to wait on the handles. Whenever a directory is created or deleted in the tree starting at C:\ , the example updates the entire directory tree. Whenever a file is created or deleted in the C:\WINDOWS directory, the example refreshes the WINDOWS directory. The FindNextChangeNotification function restarts the change notification each time the example processes a change.

DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];

// Watch the C:\WINDOWS directory for file creation and
// deletion.

dwChangeHandles[0] = FindFirstChangeNotification(
"C:\\WINDOWS", // directory to watch
FALSE, // do not watch the subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes

if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());

// Watch the C:\ subtree for directory creation and
// deletion.

dwChangeHandles[1] = FindFirstChangeNotification(
"C:\\", // directory to watch
TRUE, // watch the subtree
FILE_NOTIFY_CHANGE_DIR_NAME); // watch dir. name changes

if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());

// Change notification is set. Now wait on both notification
// handles and refresh accordingly.

while (TRUE)
{

// Wait for notification.

dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,
FALSE, INFINITE);

switch (dwWaitStatus)
{
case WAIT_OBJECT_0:

// A file was created or deleted in C:\WINDOWS.
// Refresh this directory and restart the
// change notification. RefreshDirectory is an
// application-defined function.

RefreshDirectory("C:\\WINDOWS")
if ( FindNextChangeNotification(
dwChangeHandles[0]) == FALSE )
ExitProcess(GetLastError());
break;

case WAIT_OBJECT_0 + 1:

// A directory was created or deleted in C:\.
// Refresh the directory tree and restart the
// change notification. RefreshTree is an
// application-defined function.

RefreshTree("C:\\");
if (FindNextChangeNotification(
dwChangeHandles[1]) == FALSE)
ExitProcess(GetLastError());
break;

default:
ExitProcess(GetLastError());
}
}

MSDN上的,希望能帮助你

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧