Hello,
I have created a project in C#.net and I want now that created exe run only once on any particular machine after installation.

Can any body told us how to do it.

Regards
Avadhesh

Recommended Answers

All 11 Replies

You can store one value in registry when the exe gets run first time
Now second time check if that value exist in the registry exe don't get run or immediately gets closed.
Or you can also create one file in any location invisible to the user
and second time check if that file exist exe don't get run

if still have prob
contact me at [email]snipped email[/email]

You can of course hard-code in the source the SID number of the system on which it can be run. That's all:) Do Wiki for the definition and explanation

Of course, Romasio, you fail to meet the OP's requirements.

Let's not over-complicate the scenario. I would advise the OP to use one of the methods suggested in #2.

Also if you want to shell out some cash (which I hate to do) you can use the .Net Reactor.

http://www.eziriz.com/products.htm

Then you can actually use licenses and such and it's perfect for commercial software.

Also if you want to shell out some cash (which I hate to do) you can use the .Net Reactor.

http://www.eziriz.com/products.htm

Then you can actually use licenses and such and it's perfect for commercial software.

how to create exe in visual studio 2005 which will be install only by once.

where registry located?

First of all i dont think .net provides such behaviour by default.
There are different ways for doing this, have a look at some of the suggestion below:
1. Create a registry entry and on each launch check the value for it. e.g. Check to 1 or True on first launch and exit it on the following launches.
2. Make a file and serailize a object in that file. e.g. serialize boolean value true for the first launch and by checking it never re-launch the exe.
PS. The above mentioned checks could be performed in the main function of your application i.e. prior to launch of the application

I'm a fairly junior C# programmer, so I don't know that this is the best approach ... I've used the following code to ensure that only one instance of the program is running. The approach is to set a Mutex with a unique name whenever the program runs. I check for this Mutex on startup and assume that I'm the only one running if I don't find it.

Cheers,
Bruce

private Mutex mutex;
private string mutexName = "MyProgramMutexToEnsureThatOnlyASingleInstanceIsRunning";

// code to ensure that only one copy of the software is running.
try
{
   mutex = Mutex.OpenExisting ( mutexName );
   //since it hasn’t thrown an exception, then we already have one copy of the app open.
   MessageBox.Show ( "A copy of the software is already open. Please check your system tray (notification area).",
      "My Software", MessageBoxButtons.OK, MessageBoxIcon.Information );
   Environment.Exit ( 0 );
}
catch
{
   //since we didn’t find a mutex with that name, create one
   mutex = new Mutex ( true, mutexName );
}

Sorry but now i got your main problem. The thing that you are trying to achieve or implement is termed as SingleTon pattern. The code seems fine to me but i have implemented in the following way:

static Mutex mutex;
private static bool IsAlreadyRunning()
        {
            string strLoc = Assembly.GetExecutingAssembly().Location;
            FileSystemInfo fileInfo = new FileInfo(strLoc);
            string sExeName = fileInfo.Name;
            bool bCreatedNew;

            mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
            if (bCreatedNew)
                mutex.ReleaseMutex();

            return !bCreatedNew;
        }
//Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Application
{
    static class Program
    {
        #region Private Variable

        static Mutex mutex;

        #endregion

        #region Private Method(s)
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                if (!IsAlreadyRunning())
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    frmLogin frm = new frmLogin();
                    DialogResult result = frm.ShowDialog();

                    if (result == DialogResult.OK)
                        Application.Run(new MenuForm());
                }
                else
                {
                    MessageBox.Show("A Copy of The Software is Already Open. Please Check Your System Tray (Notification Area).", "Application", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Application.Exit();
                }
            }
            catch { }
        }

        #endregion

        #region Private Method(s)

        private static bool IsAlreadyRunning()
        {
            string strLoc = System.Reflection.Assembly.GetExecutingAssembly().Location;
            FileSystemInfo fileInfo = new FileInfo(strLoc);
            string sExeName = fileInfo.Name;
            bool bCreatedNew;

            mutex = new Mutex(true, "Global\\" + sExeName, out bCreatedNew);
            if (bCreatedNew)
                mutex.ReleaseMutex();

            return !bCreatedNew;
        }

        #endregion
    }
}

By

Samdoss. J
Programmer
[email snipped]
9789017256

So you needed to post another answer to a nearly 11 month old thread that was marked answered?

Please use code tags, as well as explaination dont just throw code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.