9

So I am trying to Unit test/Integration test my code responsible for sharing a directory.

So I create my share drive and then I check if the directory exists. First locally and then via it's share name.

Assert.IsTrue(Directory.Exists(testSharePath));
Assert.IsTrue(Directory.Exists(
    String.Format(@"\\{0}\{0}", System.Environment:MachineName, testShareName));

After this I of course want to clean up after myself by removing the directory I just created. However this does not work because "...it is used by another process."

After some experimenting I found that if I remove my second Assert it works again. Am I doing something wrong? Oh, and I also noticed that if I put a 30 second sleep in there before removing the directory it also works. Wtf?

EDIT: I just revisited this issue and tried as people been suggesting in the comments to unshare the folder explicitly first. That was it. Worked like a charm.

jimmy
  • 1,981
  • 3
  • 19
  • 28
  • take a look at this alternative way to check if a Directory exist http://stackoverflow.com/questions/1395205/better-way-to-check-if-path-is-a-file-or-a-directory – MethodMan Mar 23 '15 at 17:11
  • Simple guess: it's not the fault of `Directory.Exists`, it's the SMB connection that's not closed immediately. – Lucas Trzesniewski Mar 23 '15 at 17:11
  • you are using a network connection to the drive to see if it exists -- the connection has to time out before it releases the lock. If you explicitly make the connection then you could explicitly close it. – Hogan Mar 23 '15 at 17:12
  • Pretty unlikely to have anything to do with .NET, it simply uses FindFirstFile() and always closes the handle it returns. Disable your anti-malware and try again. – Hans Passant Mar 23 '15 at 20:15
  • Are you removing the share before attempting to remove the directory? – Harry Johnston Mar 23 '15 at 20:28
  • @Hogan The problem is that I am not explicitly doing anything. I am just calling Directory.Exists. – jimmy Mar 24 '15 at 07:31
  • @HansPassant That would be very unfortunate since I am not able to change anything like that on the Bamboo agent that will run this test in the future... – jimmy Mar 24 '15 at 07:32
  • @HarryJohnston Nope. – jimmy Mar 24 '15 at 07:33
  • @jimmy - I understand that. This is why I said "If" in my comment. If as in change your code to do the following. I could have said "Rewrite your code to explicitly make the connection then you can explicitly close it." – Hogan Mar 24 '15 at 17:08
  • Here is the reference source for the system directory object if that helps : http://referencesource.microsoft.com/#mscorlib/system/io/directory.cs – Hogan Mar 24 '15 at 17:11
  • I'm surprised you can remove a shared directory at all, but certainly you won't be able to remove it while it is still in use. Try deleting the share, see if that helps. – Harry Johnston Mar 24 '15 at 21:42
  • BTW, you can answer your own questions on SO. Since no one else has put in an answer, you should go ahead and do so, to document the solution. – DWright Apr 15 '15 at 23:48

1 Answers1

2

As requested by @DWright I will answer my own question since no one else did, for documentation purpose and for clarity if other people find this post.

My problem was solved by explicitly unsharing the folder before removing it:

var share = String.Format("Win32_Share.Name='{0}'", shareName);
var managementObj = new ManagementObject(share);
managementObj.InvokeMethod("Delete", null, null);
jimmy
  • 1,981
  • 3
  • 19
  • 28