Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I check if a directory can be renamed in VB.NET?
    text
    copied!<p>My thought is to use CreateFile from kernel32 and check for sharing violations. I believe this will work because I watched the file system activity with Process Monitor while issuing a rename command from CMD that I knew would fail and the last activity was a failed CreateFile call that resulted in a sharing violation.</p> <p>This is the Process Monitor information on the call.</p> <pre><code>Desired Access: Read Attributes, Delete, Synchronize Disposition: Open Options: Synchronous IO Non-Alert, Open Reparse Point Attributes: n/a ShareMode: Read, Write, Delete AllocationSize: n/a </code></pre> <p>Using this VB code, I produced a call which gave the same information in Process Monitor but did not cause the sharing violation.</p> <pre><code>CreateFile(theDirectoryPath, _ FILE_READ_ATTRIBUTES Or DELETE Or SYNCHRONIZE, _ FILE_SHARE_READ Or FILE_SHARE_WRITE Or FILE_SHARE_DELETE, _ Nothing, _ OPEN_EXISTING, _ FILE_ATTRIBUTE_DIRECTORY Or FILE_FLAG_BACKUP_SEMANTICS _ Or FILE_FLAG_OPEN_REPARSE_POINT, _ Nothing) </code></pre> <p>The constants are pulled from various <a href="http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx" rel="nofollow noreferrer">MSDN</a> and <a href="http://www.pinvoke.net/default.aspx/kernel32.CreateFile" rel="nofollow noreferrer">pinvoke.net</a> sources.</p> <p>If I call the above code recursively on all subfolders it will eventually cause the sharing violation, but when CMD refused to rename, it did not recurse. </p> <p><strong>Yes, I know I could just try and catch the exception.</strong> But the point at which I want to know if the directory can be renamed and the point at which I want to rename the directory are not the same.</p> <p><strong>EDIT:</strong></p> <p>There may have been a source of confusion in this question. I am not concerned with permissions; I am concerned with file locks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload