English 中文(简体)
如何检查是否可以删除档案
原标题:How to check if a file can be deleted

I want to use it during uninstall procedure to warn the user ahead. The procedure should work for W2000+, hence Vista API s are not allowed.

似乎可以解决一些冲突:

if( GetFileAttributes( lpPath ) == INVALID_FILE_ATTRIBUTES )
{
    // File does not exist
}
else
{
    BOOL bCanDelete = FALSE ;
    HANDLE hFile = CreateFile( path, 
        GENERIC_WRITE /*|DELETE*/, 
        0 /*FILE_SHARE_DELETE*/, 
        NULL, 
        OPEN_EXISTING, 
        FILE_ATTRIBUTE_NORMAL, 
        NULL 
    );
    if( hFile != INVALID_HANDLE_VALUE )
    {
        DWORD size = 10000 ;  // a number > file size allowed
        if( size != INVALID_FILE_SIZE )
        {
            if( LockFile( hFile, 0,0, size,0) )
            {
                UnlockFile( hFile, 0,0, size,0) ;
                bCanDelete = TRUE ;
            }
        }
        CloseHandle( hFile ) ;
    }
}

Namely it detects these situations: a) Deleting running exe file b) Deleting opened pdf

Using GENERIC_WRITE|DELETE seems to behave similarly. Using DELETE alone works for the case b), but not for a).

我没有积极证据表明,洛克菲利(洛克菲)会迎合任何有意义的冲突,但认为确实如此。

没有人有更好的想法?

问题回答

第一点:除非你采取步骤防止这种情况,否则,在你测试的时间和你试图根据试验采取行动的时间之间(例如,在你试图检查你可以删除之后,用户可能会将其改为仅读)。

为了取得有意义的结果,我不使用删除的碎片删除文档,而是使用<条码>CreateFile和<条码>。 如果你能够以这种方式打开档案,那就给你所赢得的能够删除的极好奇迹。 如果你可以这样开张,那么简单地关闭处理程序将删除文件,除非文件还具体指明了FILE_SHARE_DELETE<>。 (即使如此,在最后处理完档案时,档案将被删除,因此如果立即删除,很快就会删除。)

我不是C++方案管理员,但你可以尝试重新命名这一档案。 如果你能够这样做,你或许可以删除。





相关问题
how to reliable capture display setting changed

static void Main() { // Set the SystemEvents class to receive event notification when a user // when display settings change. SystemEvents.DisplaySettingsChanged += new ...

Why use CComBSTR instead of just passing a WCHAR*?

I m new to COM. What exactly is the advantage of replacing: L"String" with CComBSTR(L"String") I can see a changelist in the COM part of my .NET application where all strings are replaced in this ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Statically linking Winsock?

I m using Winsock 1.1 in my project. I include wsock32.lib in "Additional Dependencies". I m looking at the DLL project using depends.exe and notice that the DLL depends on wsock32.dll. How can I ...