简体   繁体   English

我可以在c ++中的Windows中读取文件而不锁定包含该文件的文件夹吗?

[英]Can I read a file in windows in c++ without locking folder containing the file

I have a c++ program that continuously reads a log file(ex: C:/temp/file.txt). 我有一个c ++程序,可以连续读取日志文件(例如:C:/temp/file.txt)。 However, while the program is running I want to be able to delete/rename the folder('temp') containing the file being monitored. 但是,在程序运行时,我希望能够删除/重命名包含要监视的文件的文件夹('temp')。

Currently, I'm using CreateFile(). 目前,我正在使用CreateFile()。

        CreateFile(
                  szFilename,
                  GENERIC_READ,
                  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                  NULL,
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL,
                  NULL)

I get the following error whenever I delete/rename the folder: "The action can't be completed because the folder or file in it is open in another program" 每当我删除/重命名文件夹时,都会出现以下错误:“无法完成该操作,因为其中的文件夹或文件已在另一个程序中打开”

Is there a way to read/open a file without locking it? 有没有一种方法可以在不锁定的情况下读取/打开文件?

You can create a hardlink to this file somewhere else using CreateHardLinkW and open it instead of original file. 您可以使用CreateHardLinkW在其他地方创建与此文件的CreateHardLinkW然后打开它而不是原始文件。 This way the original file (which is actually a hardlink as well) and the folder containing it can be renamed / deleted but the file itself will be still accessible. 这样,可以重命名/删除原始文件(实际上也是硬链接)和包含该文件的文件夹,但是仍然可以访问文件本身。 Note that this method implies some restrictions on type of underlying file system, hardlink location, and access rights. 请注意,此方法暗含一些对基础文件系统的类型,硬链接位置和访问权限的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM