With the .NET Framework, setting ProcessStartInfo.WindowStyle
to ProcessWindowStyle.Hidden
does the trick.
Here's a simple class I wrote to encapsulate what I want. It also incorporates the splash image trick mentioned in the comments to this question (another source), since I couldn't find any other way to actually eliminate the splash image.
public class MsaccessCompactor
{
const string DefaultPathToMsaccess =
@"C:\Program Files\Microsoft Office\Office15\MSACCESS.EXE";
static readonly Bitmap BlackPixel;
static MsaccessCompactor()
{
BlackPixel = new Bitmap(1, 1);
BlackPixel.SetPixel(0, 0, Color.Black);
}
readonly string PathToMsaccess;
public MsaccessCompactor(string pathToMsaccess = DefaultPathToMsaccess)
{
PathToMsaccess = pathToMsaccess;
}
public void Compact(string pathToDatabase)
{
var arguments = string.Format("\"{0}\" /compact", pathToDatabase);
var startInfo = new ProcessStartInfo(PathToMsaccess, arguments);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
var pathToBmp = Path.ChangeExtension(pathToDatabase, "bmp");
BlackPixel.Save(pathToBmp, ImageFormat.Bmp);
using (var process = Process.Start(startInfo))
{
process.WaitForExit();
}
File.Delete(pathToBmp);
}
}
Note that there will still be a very short focus-defocus due to the seemingly unavoidable splash image appearance. Fortunately, it doesn't seem to markedly affect desktop usability in my very brief, very informal, self-performed acceptance testing. (And most thankfully, there is no Access UI window at all. Yay!)