Reputation: 986
my program installs my custom service and register it. Now what I am trying to do is to unregister the service and delete all files after uninstall. I am using Visual Studio and Setup and Deployment and the Installer class, I have overridden a few methods that I am presenting below:
protected override void OnAfterUninstall(IDictionary savedState)
{
base.OnAfterUninstall(savedState);
string directory = "C:\\Program Files (x86)\\MyService\\";
if (System.IO.Directory.Exists(directory))
{
string[] files = System.IO.Directory.GetFiles(directory);
foreach (string file in files)
{
System.IO.File.Delete(file);
}
System.IO.Directory.Delete(directory);
}
}
protected override void OnBeforeUninstall(IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
string path = "-u \"C:\\Program Files (x86)\\MyService\\AA_service.exe\"";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "C:\\Program Files (x86)\\MyService\\InstallUtil.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = path;
Process.Start(startInfo);
}
It does not unregister service and it does not delete application folder. Can anyone suggest me what am I doing wrong?
//edit
now it is trying to remove files but I am getting access denied error on uninstall. Files I am trying to delete are .exe, .dll and some others
Upvotes: 4
Views: 3960
Reputation: 1
using system.Threading;
static void Main()
{
string[] arguments = Environment.GetCommandLineArgs();
foreach (string argument in arguments)
{
if (argument.Split('=')[0].ToLower() == "/u")
{
ThreadStart starter = delegate { WorkThreadFunction(argument.Split('=')[1]); };
new Thread(starter).Start();
Thread.Sleep(30000);
Application.Exit();
return;
}
}
}
Upvotes: 0
Reputation: 29352
Did you add the custom actions into the MSI? If you don't have a custom action fire for your uninstall event then I'm not sure if these events will be called. Is there any reason why you are using the before and after install events instead of overriding the "uninstall" command?
If you don't call the Install function for the component, then the installer won't call the uninstall function either. You can program a messagebox into the uninstall (or a System.Diagnostics.Debugger.Attach()
) if you want to see whether the code is executing or not.
Also as a matter of portability, I highly recommend that you use the Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
command to find the program files directory. This will port across x64 and x86 as well as any future revisions to the program files directory in the future.
Secondly I would use the Path.Combine
function as well to safely merge the folders together.
EDIT: I think you may be misusing the installation procedure. The custom action of the installer is to "register" the service once it has been installed by the MSI. It is also to unregister the service before it is then deleted by the MSI.
Go get a copy of WIX or use the MSI builder in Visual Studio. Drop your project output for your service into the project, setup the custom actions to call on your service exe and then the MSI will handle the install/uninstall for you. THe custom action will be called and register/unregister your service with the cache.
Be very careful though, if you need to upgrade there is a bug in the behaviour of the service installer, it is not able to succesfully upgrade or downgrade without you wiring the MSI properly to handle all the sequences that can occur.
Upvotes: 2