How to write code to run once and only once

I’m looking for a way to write code so that the first time you run the program, this code segment runs once and only once. If the program is closed and started again, the code will not work again. It will start again only if the entire application is uninstalled and mounted from scratch.

The configuration file or flag in the database is not a solution to this problem.

I read something somewhere (which I apparently cannot find) to somehow start something once, and then it could essentially compile the variable into a binary file that will be used forever and is always.

Thanks for any help I can get from this!

+3
source share
8

-. , , , , :

  • ( )

  • , , , , .

  • - , - - , .

  • - , DLL . - , , - , , permssion, (, .)

, . , , , .

(, ), , .

, , , neverrun.txt. . , , . , .

. ( ), Program Files . , , .

+6

, , , ADS.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        private static string ADS_Part = "RunOnceFlag";
        private static uint FILE_FLAG_BACKUP_SEMANTICS = 0x2000000;
        private static uint CREATE_ALWAYS = 2;
        private static uint OPEN_EXISTING = 3;
        private static uint FILE_SHARE_READ = 0x1;
        private static uint GENERIC_READ = 0x80000000;

        static void Main(string[] args)
        {
            if (IsFirstEverRun())
            {
                System.Diagnostics.Trace.WriteLine("First run");
                SetRunOnce();
            }
            else
            {
                System.Diagnostics.Trace.WriteLine("I've been run at least once!");
            }
        }
        private static bool IsFirstEverRun()
        {
            //Current running EXE (assumes not being loaded from another program)
            string P = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string ADSFile = P + ":" + ADS_Part;

            //Try opening the ADS
            using (var FH = CreateFile(ADSFile, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero))
            {
                //Return whether its a valid handle or not
                return FH.IsInvalid;
            }

        }
        private static void SetRunOnce()
        {
            //Current running EXE (assumes not being loaded from another program)
            string P = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string ADSFile = P + ":" + ADS_Part;

            //Create the ADS. We could write additional information here such as date/time first run or run counts, too
            using (var FH = CreateFile(ADSFile, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, CREATE_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero)){}
        }


        [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName,
            uint dwDesiredAccess,
            uint dwShareMode,
            IntPtr lpSecurityAttributes,
            uint dwCreationDisposition,
            uint dwFlagsAndAttributes,
            IntPtr hTemplateFile);
    }
}
+4

, , , . / , , /.

:

  • . (: )
  • HKLM/HKCU. (: )
  • app.config web.config (. , , , , ).

, , /.

+1

, , :

  • Windows. , , . , . ( , , ).

. , , , :

if(!serviceInstalled)
{
    CodeThatRunsOnceAndOnlyOnce();
    InstallService();
}

, , - , , - . , .

+1

, Asp.Net, Winforms, , Asp.Net - , ?

0

WinForms , ASP.NET, Application_Start() Global.asax. ( , .)

0

...

, node ,

:.setting.defaults. ...

and also set it, after setting your variable you need to call save ()

0
source

you can write it in a script, so after installing the application, the script is executed and then deleted. or have a gadget that you launch manually, either after the first installation, or when you want to reinitialize your application. this way you can restore the new installation by running the gadget.

0
source

Source: https://habr.com/ru/post/1781535/


All Articles