Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Process.Start and manually running exe

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));
}
like image 592
Brandon Avatar asked Jul 12 '26 21:07

Brandon


1 Answers

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.

like image 78
FlyingStreudel Avatar answered Jul 14 '26 10:07

FlyingStreudel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!