3

Big picture: I wrote an Autodesk inventor addin. On the close event of Inventor it launches another program I'll call purgeworkspace.exe. purgeworkspace.exe waits until Inventor is closed, then deletes files that Inventor was using.

Here is the relevant addin code which calls purgeworkspace.exe.

//Run as admin
if (System.Environment.OSVersion.Version.Major >= 6)
{
    Process p = Process.Start(Properties.Settings.Default.exePath);
    p.StartInfo.Verb = "runas";
}

If purgeworkspace.exe is launched from the addin, I get a IOException saying files are in use. However if I run purgeworkspace.exe manually by double clicking the exe, it works perfect every time. My belief is that this is due to permissions, but I don't know what. As you can see I'm already trying to run my exe as admin, but it isn't solving the problem.

purgeworkspace.exe works exactly as I intend when I launch it manually. How can I call it programmatically so it runs the same way?

Edit: Here's some code to demonstrate how purgeworkspace waits for inventor. Keep in mind this code is run both when I launch it manually and when it's run from the Inventor addin.

static void Main()
{
   while (IsProcessOpen("Inventor"))
   {
       System.Threading.Thread.Sleep(50);
   }
}

private static bool IsProcessOpen(string name)
{
   return Process.GetProcesses().Any(clsProcess => clsProcess.ProcessName.Contains(name));
}
Brandon
  • 1,058
  • 1
  • 18
  • 42
  • Verify that the processes current directory is what it should be, or that it's not used. – Servy May 06 '13 at 14:53
  • You have "Run as admin" as a comment, but I don't see where the elevation happens. Is that what the .Verb is suppose to do? Do you have username and password in there too? – Doug Dawson May 06 '13 at 14:54
  • @Darkwater23 I made an assumption the top answer from here would do it. It's possible I'm wrong. http://stackoverflow.com/questions/2532769/how-to-start-a-process-as-administrator-mode-in-c-sharp – Brandon May 06 '13 at 14:55
  • Going out on a limb here, but I think its saying the files are still in use. Which means it won't matter if you're administrator or not. Might be worthwhile to figure why it thinks they're still in use. – FlyingStreudel May 06 '13 at 14:57
  • @Servy I'm not sure I follow. I'm already checking to see if inventor is closed. If Inventor was still using the files, my program wouldn't work when I run it manually either. – Brandon May 06 '13 at 14:57
  • @FlyingStreudel If the files were actually in use then it wouldn't work when I manually run my program either. – Brandon May 06 '13 at 14:59
  • @Brandon Perhaps the execution is different when running as an add-in? I'm fairly certain that if it says the files are still in use, they're still in use. – FlyingStreudel May 06 '13 at 15:00
  • Maybe you could add a delay, just to test. Have it thread sleep for a second or two and see if that makes a difference. Running as an add-in might mean that the Inventor has to wait for your add-in to finish executing and is therefore still holding onto the file you want the add-in to delete. – Doug Dawson May 06 '13 at 15:05
  • @Darkwater23 Edited my question to show that I'm already checking if Inventor is closed in the same way when launched manually and when launched from the addin. – Brandon May 06 '13 at 15:09

1 Answers1

1

Regardless of what precautions have been taken, something out there is still holding a lock on your files. Perhaps the process that has the open file handle is not named like "Inventor"?

You could use this question to figure that out.

Community
  • 1
  • 1
FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55
  • According to Process Explorer, the GUI version of handle.exe, nothing is using the files I'm trying to delete. I understand your reasoning, but I believe the problem is more than just a file in use. – Brandon May 07 '13 at 12:07
  • Turns out you weren't wrong. The exe I was launching from Inventor was running from inside the folders i was trying to delete (because that was inventor's working directory). Thus, my exe was the file in use that it was trying to delete. It didn't show in Process Explorer because I only thought to check which files were in use when inventor closed, not while my program ran. – Brandon May 07 '13 at 14:34
  • @Brandon I'm glad you figured it out! – FlyingStreudel May 07 '13 at 15:00