C #: Limit console application use

We have a product that is a C # console application. Is it possible to limit it to running only from the command line? In other words, users will not be able to call this from a script or other application.

If so, some sample code would be much appreciated. Thank.

+5
source share
4 answers

You can check the process of creating your application using the code here: http://msdn.microsoft.com/en-us/netframework/aa569609.aspx#Question3 . To start at the DOS prompt, the parent process must be cmd.exe. Note, however, as Colin pointed out, this can be easily circumvented using the script package. You can also disable this by making sure the cmd.exe command line arguments are null. For this you will need to use WMI:
http://skysanders.net/subtext/archive/2010/04/11/using-wmi-to-fetch-the-command-line-that-started-all.aspx
You should also verify that the cmd.exe image is located in the system32 folder.

+3
source

I don’t think you can tell the difference.

, . , :

1. type app name into Command Prompt:     cmd.exe
2. call app from batch script:            cmd.exe
3. Double click on app or shortcut:       explorer.exe
4. type app name into Run dialog box:     explorer.exe   

1. , , 2. , script ( 1- script )

(, - StackOverflow?)

+3

@swisston, , " ". , . mutex . . ( ): ;) , ;)

Edit: . :

Mutex mutex = new Mutex(true, "MyPermissions");

, :

    static bool CheckPermissions()
    {
        try
        {
            Mutex mutex = Mutex.OpenExisting("MyPermissions");
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }

, CheckPermissions false ;)

+1
source

I don’t agree with what you are trying to do, but here is an idea that could work: require some kind of user input at the beginning of the program, maybe some CAPTCHA (difficult to do in a team but theoretically possible. Think of ASCII art )

0
source

All Articles