static void Main(string[] args) {
/* We will assume that settings of all AutoCAD were
* damaged and it is necessary to do the recovery
* of them. */
/* The AutoCAD applications set which was installed
* on the current machine. */
InstalledProduct[] installed_aps = LocalizedProduct
.GetInstalledProducts();
Console.WriteLine("Installed AutoCAD set:");
foreach (var lm_app in installed_aps) {
Console.WriteLine("{0}\n{1}\n\n", lm_app
.Product.Name, lm_app.Location);
}
/* Get the existing registry keys of AutoCAD
* products in HKCU. */
InitializedProduct[] init_apps = LocalizedProduct
.GetInitializedProducts();
Console.WriteLine("The AutoCAD applications which"
+ " were launched by current user at least once:");
/* Recovery each AutoCAD current user settings. */
foreach (var cu_app in init_apps) {
Console.WriteLine("{0}\n{1}\n\n", cu_app
.InstalledProduct.Product.Name, cu_app
.InstalledProduct.Location);
try {
if (Directory.Exists(cu_app
.RoamableRootFolder)) {
Directory.Delete(cu_app
.RoamableRootFolder, true);
}
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
try {
if (Directory.Exists(cu_app.LocalRootFolder
)) {
Directory.Delete(cu_app.LocalRootFolder
, true);
}
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
RegistryKey rk = Registry.CurrentUser
.OpenSubKey(cu_app.InstalledProduct
.SubkeyName, false);
bool exists = rk != null;
if (exists) {
rk.Close();
((IDisposable) rk).Dispose();
Registry.CurrentUser.DeleteSubKeyTree(
cu_app.InstalledProduct.SubkeyName);
}
/* Now I want to force the install of AutoCAD
* components which I deleted above. I.e. I
* recovery the damaged settings to the state
* which was directly as soon as AutoCAD was
* installed. */
/* The absent components will be installed
* during the next AutoCAD session. */
using (Process proc
= new Process
()) {
ProcessStartInfo info
= new ProcessStartInfo();
info.FileName = Path.Combine(cu_app
.InstalledProduct.Location, "acad.exe")
;
info.UseShellExecute = false;
proc.StartInfo = info;
/* It initializes installing the components
* also. */
proc.Start();
/* When all components will be installed
* the AutoCAD application will be
* launched. So, I want to catch this
* moment and close the `acad` process.*/
/* It doesn't close the AutoCAD main
* window: */
while (proc.MainWindowHandle == IntPtr
.Zero) {
proc.Refresh();
}
proc.CloseMainWindow();
/* It interrupts also the components
* installing: */
// proc.Kill();
proc.Close();
}
}
}