views:

73

answers:

3

I need my application to ask the user to browse to a particular file, save that files location and subsequently write a string from a TextBox to it.

However, I only need my end-user to browse to the file the first time the application launches. Only once.

Here lies my dilemma, how can I have my application remember if it was the first time it launched?

A: 

Save the file chosen in the registry, or in a configuration file in the user's Documents and Settings folder.

To get to your local program's path, use:

string path = Environment.GetFolderPath(Environment.LocalApplicationData);
Aviad Ben Dov
How would I do this?
Sergio Tapia
There you go, example in place!
Aviad Ben Dov
A: 

I would use the Registry to add an entry for "SavedFileLocation" for your application.

For a tutorial on using the registry, check here.

Then you can check if the key exists, if not present the dialog.
If the key exists, you should check for existence of the file. If the file does not exist, you should probably present this information to the user, and ask them if they want to create a new file there, or choose a new location.
Otherwise, take that value and keep it for runtime.

CODE:

AppInitialization()
{
    RegistryKey appKey = Registry.CurrentUser.OpenSubKey(
        @"Software\YourName\YourApp"
        ?? Registry.CurrentUser.CreateSubKey( @"Software\YourName\YourApp" );


    this.fileLocation = appKey.GetValue( "SavedFileLocation" )
        ?? GetLocationFromDialog()
        ?? "DefaultFileInCurrentDirectory.txt";
}

private static string GetLocationFromDialog()
{
    string value = null;

    RegistryKey appKey = Registry.CurrentUser.OpenSubKey(
        @"Software\YourName\YourApp"
        ?? Registry.CurrentUser.CreateSubKey( @"Software\YourName\YourApp" );

    using( OpenFileDialog ofd = new OpenFileDialog() )
    {
        if( ofd.ShowDialog() == DialogResult.OK )
        {
            value = ofd.File;
            appKey.SetValue( "SavedFileLocation", value );
        }
    }

    return value;
}
maxwellb
Ok, this isn't "If it's the first time the application is launched", but it's "If I have a 'valid' file location remembered."You can adopt this to save a setting of "NotFirstTime" exist/not exist.
maxwellb
+5  A: 

I think you want a folder, not a file, but that is besides the point.

You can use a UserSetting (See Project properties, Settings) and deploy it with an empty or invalid value. Only when you read the invalid value from settings do you start the Dialog.

This is on a per-user basis.

You can use the Registry in .NET but you really want to stay away from that as much as possible. The fact that the library is not in a System namespace is an indicator.

Henk Holterman
I think file makes sense. OP is remembering a file to append/write to.
maxwellb
Your suggestions sounds very sound, however it's Chinese to me! I don't know what you mean with "deploy it with an empty or invalid value". Care to elaborate a bit?
Sergio Tapia
have a look at http://msdn.microsoft.com/en-us/library/k4s6c3a0.aspx for more information on user settings. Have a look at http://msdn.microsoft.com/en-us/library/wabtadw6.aspx for info on using the designer to manage them.
Nader Shirazie
nader provides some useful links, just create a setting Userpath, type string and UserScope, leave the value blank. In your program check Properties.Settings.Default.UserPath, if it is empty start the dialog and afterwards fill the Uesrpath property and call Settings.Default.Save()
Henk Holterman