0

I have developed this c# program which is hardware binded. Incase the program runs on a different hardware, then it should delete itself. This seems to be working on my 2 of my home PCs, however not on my Work PC. Can you please tell me what is the issue?

Following code is part of App.xaml.cs.

            if (thisComputerLicense == decryptedLicense)
            {
               // Valid Hardware 
               // Instanciate the view you want to display and show it
               MainWindow mainWindow = new MainWindow();
               mainWindow.Show();
            }
            else
            {
                //Invalid hardware, delete program
                MessageBox.Show("Invalid Hardware"); //This message is shown on my work computer as well
                Application.Current.Shutdown();

               Info.Arguments = @"/C choice /C Y /N /D Y /T 3 & Erase /f /q " +
                                   Directory.GetCurrentDirectory();

                Info.WindowStyle = ProcessWindowStyle.Minimized;
                Info.CreateNoWindow = true;
                Info.FileName = "cmd.exe";
                Process.Start(Info);

I m running this program on windows 10 with .Net framework 4.8.

Salman Saleh
  • 173
  • 2
  • 9
  • 4
    Does the user you're running the app as have permissions to delete the files in the way that you are attempting? Have you tried running the app as an admin? – emagers Apr 26 '22 at 15:38
  • 2
    Probably a permissions thing, as @emagers says - check it by running it with admin rights. – Andrew Corrigan Apr 26 '22 at 15:53
  • 1
    What happens if you run the same command on your work PC? – Dash Apr 26 '22 at 16:23
  • 1
    Erasing`Directory.GetCurrentDirectory` could be dangerous as the working directory might not be the program directory. Which would result in you trying to delete an unrelated directory. And even if it is the same, what if I just copy the program/install it into the desktop. Now, your program would try to delete the desktop. You should better forget about the program erasing itself. This could even have legal repercussions if you delete stuff without permission and brick the system. – ckuri Apr 26 '22 at 16:28
  • Thanks, As far as I remember, I tried running the app as admin on my work computer but that didn't worked. However I will give it a try again tomorrow. @ckrui Thanks for the tip, I have replaced it with "AppContext.BaseDirectory", which is much better (https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) Secondly, my users will know about this app deletion (incase of different hardware). – Salman Saleh Apr 26 '22 at 17:18
  • Would it not just be easier to quit with a message if the hardware is wrong. Self destructing the program is a minor inconvenience in a day and age where files can be copied with a couple of key presses – Caius Jard Apr 26 '22 at 20:16
  • Update: I tried the application in administrator mode but to no avail. However files do get deleted, if I try this command in cmd. Maybe this is due to OS, my office work computer is using an older version of windows 10 pro (14393). – Salman Saleh Apr 27 '22 at 14:16
  • @CaiusJard Yes, after these issues I m considering that route if this deletion technique doesn't work. Thanks – Salman Saleh Apr 27 '22 at 14:20

0 Answers0