2

Assume I have a String Field in my WinApp like below :

public string UsingTheApplicationFrom = "Not Yet";

Now , I wanna to change the value of the field forever, in first running of my application. e.g : UsingTheApplicationFrom = "‎Friday, ‎January ‎21, ‎2011";

Is it possible in C# WinApp ?
Could you please guide me?

Edit:
I don't wanna use Database or file or Registry or something else.

Mohammad Dayyan
  • 18,338
  • 35
  • 143
  • 207
  • 1
    Is anyone else confused? – Tony Abrams Jan 21 '11 at 14:34
  • 1
    I think he wants to set a string the first time his application is run, then have that value persist indefinitely... – Donut Jan 21 '11 at 14:35
  • @Tony: VERY!... @MOhammad: what do you mean for always? For the life of the application? Persist to file? Throw us a bone. – capdragon Jan 21 '11 at 14:36
  • Where my question is ambiguous, I can describe it more ? – Mohammad Dayyan Jan 21 '11 at 14:36
  • 1
    What sort of security do you need for this? Is it okay if the user could find this value and change it with some work, or are you making a time-limited demo version of your application? – Justin Jan 21 '11 at 14:37
  • @capdragon: `for always` I mean, after changing , whenever a user runs my App , see `"Friday, ‎January ‎21, ‎2011` in `UsingTheApplicationFrom` , not `Not Yet` – Mohammad Dayyan Jan 21 '11 at 14:39
  • 1
    @Justin: I don't wanna make a time-limited for the App, I need a type of security for my application that only the first system (that runs the application for the first time) could use my application – Mohammad Dayyan Jan 21 '11 at 14:42
  • @Mohammad - Consider editing your question to add "I need a type of security for my application that only the first system (that runs the application for the first time) could use my application" as part of your question. – Justin Jan 21 '11 at 15:36

6 Answers6

3

I am taking a guess, but by "always" I think he means "forever". You could easily create a key in the App.config of your application and populate it on the first run.

<add key="UsingTheApplicationForm" value="Not Yet"/>

On first run, update it to -

            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["UsingTheApplicationForm"].Value = DateTime.Now.ToShortDateString();
            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.
            ConfigurationManager.RefreshSection("appSettings");

On second run check if the key value is equal to "Not Yet". If it is not, it means that this is not the first run.

Mayank
  • 1,401
  • 2
  • 11
  • 29
  • Does `App.config` reset if the user runs the application in another windows or system ? – Mohammad Dayyan Jan 21 '11 at 15:18
  • 1
    If you are copying the same app.config file to each system, then app.config won't get updated. However if you are doing a fresh install (e.g. from a installer), then app.config would be brand new and hence each system value would be different. – Mayank Jan 21 '11 at 15:22
3

You are looking for "a type of security for my application that only the first system (that runs the application for the first time) could use my application". I think you mean that each copy of the software you sell may only be installed on one computer at a time.

You have two problems to solve:

  • How to generate a unique identifier for the computer
  • How to store the identifier value

You have several options to use for a unique identifier, none of which are great. Be prepared for support requests from customers when they change their computer hardware, when their computer breaks, or when they want to move the software from one computer to another. A decent-looking method to compute a unique identifier is this article (mirror link since the code project article is not available).

I would recommend just storing this identifier as a string in the app.config file (using Properties.Settings, start at this link for more information). It will be visible in plain text, but how would an unlicensed user know what value to change it to for their machine when it looks like "4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9"? If it doesn't match direct them to your website/telephone for support.

You still have the problem of deciding when to set this value -- I would advocate for setting it as part of an installer instead of saving it the first time the program is run, since then you still have a problem to determine when the program is first run. The installer might need some sort of registration code and a method to communicate with a central licensing server. (Yes, this does get complicated -- how determined do you think people might be to hack your licensing?)

Justin
  • 6,251
  • 3
  • 33
  • 52
2

I think maybe you want to use DateTime, and particularly, DateTime.Now - as with comments to the question however, I'm not properly sure.

So, something like:

UsingTheApplicationForm = String.Format("{0:dddd, MMMM d, yyyy}", DateTime.Now); 

The 'always' part is very confusing though, I'm just going to buy into my own translation, where always = each startup.

I would also conjecture that, given the property/variable name of UsingTheApplicationForm, this value is an indicative field, rather than a descriptive one; for this reason, may just using DateTime.Now without any formatting, or even just a boolean (depends what fits your situation) fit the bill?

Edit:

For information on using this method, or any of the others, along with persisting this data for the next run, see the following question and answers...

Editing app.config in execution time using the same App

Community
  • 1
  • 1
Grant Thomas
  • 42,191
  • 8
  • 81
  • 120
1

I like this way: http://windowsclient.net/blogs/suryahg/archive/2008/08/11/persist-winforms-application-settings.aspx

capdragon
  • 13,443
  • 22
  • 96
  • 145
  • Instead of using XML Serializer, wouldn't it be easier to use App.config to persist values? But again, as Justin had pointed out all this depends on the level of security that is needed. – Mayank Jan 21 '11 at 14:40
1

There's no way to do it with nothing more than a string object, but you can create a simple object that does it:

public class ApplicationInfo
{
    private ApplicationStartTime()
    {
        this.StartTime = new DateTime().Now;
    }

    public DateTime StartTime
    {
        get; private set;
    }

    public Create()
    {
        return new ApplicationStartTime();
    }
}

I haven't compiled that, so there may be a minor syntax error or two. You would invoke it like so:

var applicationInfo = ApplicationInfo.Create();

Debug.WriteLine(applicationInfo.StartTime.ToString());
Chris B. Behrens
  • 6,384
  • 8
  • 41
  • 67
1

In your program.cs or main entry point for the program declare a public static datetime.

when the program is first run you can set the date time and access it in future.

public static Datetime m_StartDate = DateTime.now;

public static void Main(args)
{
m_StartDate = DateTime.Now;
}

then in your other forms (assuming you added the code to Program.cs)

txtStartTime.Text = Program.m_StartDate.toString();
WraithNath
  • 16,543
  • 8
  • 50
  • 79
  • Thanks dude, But you couldn't understand me. – Mohammad Dayyan Jan 21 '11 at 14:43
  • 1
    ah i see, you only want one instance of the app to be run anywhere at one time? – WraithNath Jan 21 '11 at 14:44
  • in your answer the value of `m_StartDate` will change in next running. I wanna change the value of `m_StartDate` for always. – Mohammad Dayyan Jan 21 '11 at 14:45
  • ah ok, so you only want to set the value once - for ever. Is this machine specific? You could check for a value in the registry, if its not there then create it, and if it is there then do nothing. this would be machine specific. – WraithNath Jan 21 '11 at 14:47
  • If I use Registry the value of `m_StartDate` will change if a user runs the Application in another system or another Windows !!! Isn't it ? – Mohammad Dayyan Jan 21 '11 at 14:49
  • 1
    yes, If you want it to be world wide then you are going to have to host somewhere to store the data. if you had a central database then you could store it on there but then your app would have to use a web service call on start or similar to check if the entry already exists in your database. – WraithNath Jan 21 '11 at 14:52