2

My speech recognition project include 2 forms form1 & form2. Form2 is my main form but before loading form2 my program take a variable value from user through form1 and pass it to form2. It means at start my program opens form1 takes value & pass it to form2 then form2 is shown. Now my question is how to make form1 load only at programs 1st launch after installation and after 1st launch directly form2 is loaded thereafter? means form1 should not be loaded after that..

  • you can store a value in your application configuration and override it after the first time...`if(appConfigFirstStartValue==true){//show form....appConfigFirstStartValue = false;}` – DrCopyPaste Feb 27 '14 at 16:19
  • Are the "variables" from form1 stored anywhere? Like in a file, the registry or a database? – EkoostikMartin Feb 27 '14 at 16:20
  • @EkoostikMartin no the variables are not stored anywhere.the variable is implicitly created & then passed via get & set method – user3289174 Feb 27 '14 at 16:24
  • @DrCopyPaste I didn't got it can you please provide me with details? – user3289174 Feb 27 '14 at 16:24
  • But you still need to pass that original value to form2. Right? – Steve Feb 27 '14 at 16:25
  • I guess getting the values for form2 is not your issue but just ensuring that form1 is only executed once, sorry I cannot write up a "real" answer now, but here is a link as to how you can handle app.configs, in essence you write the default app.config(shipped with your installation) manually and include it and then read and write to it later on: http://stackoverflow.com/a/13043569/2186023 – DrCopyPaste Feb 27 '14 at 16:27
  • @Steve yes i want to pass that variable value assigned to it in its 1st attempt – user3289174 Feb 27 '14 at 16:29
  • @user3289174 you can write those values you grab from your 1st form into the app.config also (and i guess you should ;)) – DrCopyPaste Feb 27 '14 at 16:30
  • Then you need to store it in some file and retrieve before starting the first form – Steve Feb 27 '14 at 16:31
  • @Steve thank you buddy can you give me a basic overview program of this? – user3289174 Feb 27 '14 at 16:36
  • @DrCopyPaste thank you sir..i will study it from your mentioned link – user3289174 Feb 27 '14 at 16:57

4 Answers4

2

I suggest to use a simple textfile where you could store the input value recorded the first time your app starts, then, check if the file with the value exists and read it back.

For Example

string customValue = string.Empty;
string appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
appData = Path.Combine(appData, "MyAppName");
if(!Directory.Exists(appData))
    Directory.CreateDirectory(appData);
string appDataFile = Path.Combine(appData, "MyAppNameSettings.txt");
if(File.Exists(appDataFile))
    customValue = File.ReadAllText(appDataFile);
else
{
    customValue = AskUserForTheFirstTimeValue();
    File.WriteAllText(appDataFile, customValue);
}

The file is stored in a subfolder of the common application data (C:\programdata) created to store your data files. You check if the file exists at first launch. If the file exists you read its content (assumed to be a simple string here), if the file doesn't exist then you ask the input and store the value for the successive runs of your app.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • thank you bro. if i use the txt file as a embedded resource then? – user3289174 Feb 27 '14 at 16:39
  • Not really necessary. If you have a default value you could easily change the above approach setting the customValue to your default. Otherwise I can't see any reason to use an embedded resource for this. – Steve Feb 27 '14 at 16:40
  • ohk i got it but what about the launching form1 only at 1st time? – user3289174 Feb 27 '14 at 16:56
  • Move the code that writes the file inside the form1 and launch it instead of AskUserForTheFirstTimeValue example. – Steve Feb 27 '14 at 16:58
0

You can create a Registry Key in Windows Registry (regedit), and when you start your program verify if its exists and the value.

Link about Registry Keys:

http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey(v=vs.110).aspx

Only a Curious Mind
  • 2,807
  • 23
  • 39
  • 2
    storing values in application configuration file is more reliable than storing in registry as some useres may not have permissions to read/write values in or from registry IMO. – Sudhakar Tillapudi Feb 27 '14 at 16:23
  • Mine is just an opinion and perhaps your answer is correct, but leaving user programs mess with registry has been a terrible mistake by Microsoft. Nowadays there are better options (config files, xml, a plain file are all better options) – Steve Feb 27 '14 at 16:24
  • @Steve: sidenote, apart from `registry` and `config files` is there any other better approach to tackle these kind of problems. – Sudhakar Tillapudi Feb 27 '14 at 16:27
  • @SudhakarTillapudi I usually opt for a custom XML file saved in the ProgramData folder or User personal application folder (depending on the kind of settings of course) – Steve Feb 27 '14 at 16:29
  • and i see many thirdparty widows applications relay on windows `registry`( pleae correct me. if i'm wrong), if there are more settings to save. how they handle the issues with User access permissions and things like that? – Sudhakar Tillapudi Feb 27 '14 at 16:31
  • lol, downvote my answer? Its a correct answer you can not downvote. – Only a Curious Mind Feb 27 '14 at 16:33
0

You should have a settings file that keeps a variable like IsFirstRun = true; This application should be distributed and compiled with this file, at start up you should read this file and if you encounter the true state you should load the appropriate forms. You should also ensure that the value is immediately set to false for the second launch condition.

have a look at .net's setting class.

Spook Kruger
  • 367
  • 3
  • 15
0

There are two ways I can suggest:

First one

Use the application configuration file:

Creating such file in c# is pretty easy, per usual if you start with a standard Windows Forms Project Template you will most likely already have an app.config in that project, if not, follow instructions from here (under Application Configuration Files).

Add a simple boolean value to the file like so (that appSettings-node is created under the root configuration-node:

<appSettings>
    <add key="FirstRun" value="true" />
 </appSettings>

In your first form, where you do your application configuration you add code to manipulate your app.config programmatically using the ConfigurationManager-Class for the event that configuration has been finished and can be saved (probably some button click, note that you will need a Reference to System.Configuration):

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

bool myVal = bool.Parse(config.AppSettings.Settings["FirstRun"].Value);

if (myVal)
{
    // MessageBox.Show("yep, its true");

    config.AppSettings.Settings["FirstRun"].Value = false.ToString();
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
}
else
{
    // MessageBox.Show("na, its not true anymore.");
}

Second one

Use a property grid for your application configuration:

Since from your description it seems you only use the first form to enter some application configuration values, I would recommend using the PropertyGrid-Control for that.

That control can be easily bound to an object that exposes some properties (what a surprise) that are then used to render a standardized control containing captions and value selection controls dependend on the property's types, for example:

public class Settings
{
    public int MyProperty1 { get; set; }

    public string MyProperty2 { get; set; }
}

Then you check at your program start, whether the configuration file (you define the path) exists, and if so, try to deserialize an object from its xml (sample below works for primitive types, for more complex ones you might have to extend the serializer, but you get the idea).

That serialized object represents your saved application settings, so you do not need to open up the propertygrid-form anymore.

If no file could be found the grid gets initialized with a simple new constructed object and you show that form:

this goes into your initialization code:

FileInfo file = new FileInfo("SaveHere.xml");
if (file.Exists)
{
    using(StreamReader reader = new StreamReader("SaveHere.xml"))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Settings));
        Settings mySettings = (Settings)serializer.Deserialize(reader);
        reader.Close();
    }
}
else
{
    this.propertyGrid1.SelectedObject = new Settings();
    // show form code
}

this code goes into the event code where you want to save your configuration

using(StreamWriter writer = new StreamWriter("SaveHere.xml",false))
{
    XmlSerializer serializer = new XmlSerializer(this.propertyGrid1.SelectedObject.GetType());
    serializer.Serialize(writer, this.propertyGrid1.SelectedObject);
    writer.Close();
}
DrCopyPaste
  • 4,023
  • 1
  • 22
  • 57
  • thank you so much but there is one more thing that after 1st time the variable value which where sated 1st time should load directly to form 2 – user3289174 Mar 01 '14 at 17:40
  • @user3289174 you have that saved in a file then, when using the first approach, you can read your saved values (in your second form) from `config.AppSettings.Settings[""].Value` when using the second approach you just deserialize your xml-file to an object (in your second form) `Settings mySettings = (Settings)serializer.Deserialize(reader);` your settings then are inside that very object `mySettings` or did I misunderstand you – DrCopyPaste Mar 03 '14 at 08:33