How to verify that the executable has been changed? C#

Asked

Viewed 121 times

1

Someone could help me, I’m trying to check if the execultavel was renamed, like

Demo.exe (has to be like this always)

if change it give error The Archive must have the name 'Demo'.

and not allow opening the file if it does not have the right name.

the problem I’m not knowing how to do the check...

in my code you can check if it is in the folder, but if you have a Demo and another with any name it opens...

follows the code.

private string ExecutablePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

if (File.Exists(ExecutablePath + @"\Demo.exe"))
            {

            }
            else
            {
                MessageBox.Show("O Arquivo deve ter o nome \'Demo\'.", "Error");
                Environment.Exit(0);
            }

from now on thanks.

  • this way the most that will do is to check if the file exists in the folder, can not see to be changed if you do not know the name. The closest to this would be to make an application that is running all the time and monitor the folder to see when something changes, but if it stops for some reason it will lose the changes. That’s what class is for FileSystemWatcher

  • No no, I wanted to soh allow the file to open if it has the name Demo.exe, otherwise the error. whenever open check whether the name is correct, if changing it does not execute.

  • 2

    Ah understood, see if this resolves, then I can post a more detailed answer: if (System.AppDomain.CurrentDomain.FriendlyName == "Demo.exe")

  • would be +/- this msm, however I am not able to put this code in Demo.exe, if it is in another right form, but in it does not open the file itself, but it generates the error if you change the name...

  • but if it works the FrindlyName you already have to compare, now if you are giving error is another problem. Which error occurs?

  • @Ricardopunctual worked, it was a mistake my kkk

  • 1

    OK, I left as answer below. good luck!

Show 2 more comments

2 answers

1

Rodrigo Pretti Fantin, the Process class has a method called Getcurrentprocess that returns a reference to the process that is currently running (its application). With this reference in hand, just refer to the Processname property.

// Obtém o nome do processo atual (esta aplicação)

    Process.GetCurrentProcess().ProcessName;

Your routine for checking would look something like this:

[STAThread]
static void Main()
{
    // Obtém o nome do processo atual (esta aplicação)
    string meuProcesso = Process.GetCurrentProcess().ProcessName;

    // Procura o processo atual na lista de processos que
    // estão a ser executados neste momento, no computador
    Process[] processos = Process.GetProcessesByName(meuProcesso);

    // Além desta instância, já existe mais alguma?
    if (processos != null && processos.Length > 1)
    {
        // Mostra uma mensagem, e termina esta instância...
        MessageBox.Show("Só pode haver um!!", "Highlander :)");
    }
    else
    {
        // Permite executar a aplicação
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

How to know the Runtime Executable name

  • this check I already use, the problem eh check if the name of . exe has been modified, it soh can open the . exe if it is Demo.exe if it is Demo2.exe it eh to generate an error and close...

1


For that you can use Currentdomain.Frindlyname

if (!System.AppDomain.CurrentDomain.FriendlyName == "Demo.exe")
{
   MessageBox.Show("O Arquivo deve ter o nome \'Demo\'.", "Error");
   Environment.Exit(0);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.