code_junkie

Best way to make it so an exe can only be run once

Nov 11th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. Private Sub Form_Load()
  2. If App.PrevInstance = True Then
  3. MsgBox "Already running"
  4. 'Do whatever you need to do before closing
  5. End If
  6. End Sub
  7.  
  8. private bool CanIStart
  9. {
  10.  
  11. try
  12. {
  13. MyAppMutex= new Mutex(false, "myAppMutex", out createdNew);
  14. if(MyAppMutex.WaitOne(0,false))
  15. {
  16. return true;
  17. }
  18. else
  19. {
  20. MyAppMutex = null;
  21. return false;
  22. }
  23.  
  24. }
  25. catch(ApplicationException ex)
  26. {
  27. // we couldn't create the mutex. // log the error if you care
  28. return false;
  29. }
  30. }
  31.  
  32. ' Test eventual mark, settings in the registry.
  33. if GetSetting("MyInstallerApp","Startup","BeenHere",0) = 1 then
  34. MsgBox "This installer was ran once already... first run the un-installer."
  35. End ' or some other code to properly exit the installer
  36. EndIf
  37. Call SaveSetting ("MyInstallerApp","Startup", "BeenHere", 1) 'leave a mark for future
  38.  
  39. ' Allow future Installer to run again
  40. Call DeleteSetting("MyInstallerApp", "Startup")
Add Comment
Please, Sign In to add comment