Question

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));
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top