Hi all,
Question regarding the settings feature in Visual C# 2008. Let's say I
create a user setting called TextEditor in the settings designer. I realize
that I can type a string value such as NOTEPAD.EXE in the Value column.
However, what if I want to make the default value something like the
following:
Environment.GetEnvironmentVariable("windir") + "\\NOTEPAD.EXE"
Is there any way to do this at design time, or what is the best way to
handle this?
Thanks for any help,
Marc Gravell - 29 Apr 2008 20:52 GMT
Personally, I'd simply use %windir%\NOTEPAD.EXE as the default, and
use Environment.ExpandEnvironmentVariables() to do the expansion on-
demand.
Marc
Clarks Computing - 29 Apr 2008 22:17 GMT
Thanks Marc for the response. I tried %windir%\notepad.exe however, I'm a
little confused. I still can't get it to return the actual Windows
directory instead of the text "%windir%". I was hoping there was a way to
enter a default setting using the designer which would execute at run-time.
I guess there's no way to do this other than assigning the values
programmatically.
> Personally, I'd simply use %windir%\NOTEPAD.EXE as the default, and
> use Environment.ExpandEnvironmentVariables() to do the expansion on-
> demand.
>
> Marc
Rene - 29 Apr 2008 21:32 GMT
I don't thinkg the ApplicationSettings class is able to evaluate the
expression and return the correct value.
Then only way I see that you can accomplish something like what you want is
to hack the "Settings.Designer" class and hardcode the changes so that it
would look something like this:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public string TextEditor
{
get
{
return System.Environment.GetEnvironmentVariable("windir") + "\\" +
((string)(this["TextEditor"]));
}
set
{
this["TextEditor"] = value;
}
}
I think thissolution is reeeeeeeeeally cheese. For consistnecy, I guess you
can add code to the "set" accsesor so that get and set are consistent (the
set and get return the same type of string with "windir" as the prefixed
path).
Keep in mind that the designer will overwrite everything every time you make
a change so you will have to constantly remember to update the file.
I would't recommend going this way but if you have no other choose well
there is an option.....
> Hi all,
>
[quoted text clipped - 11 lines]
>
> Thanks for any help,
Clarks Computing - 29 Apr 2008 22:25 GMT
Thanks, but what a pain! I thought this would be such an easy thing to do,
but it doesn't seem to be the case. What would you suggest instead of the
settings class? Is there an easier way to manage user settings for a small
project which is what I'm working on?
>I don't thinkg the ApplicationSettings class is able to evaluate the
>expression and return the correct value.
[quoted text clipped - 44 lines]
>>
>> Thanks for any help,
Rene - 30 Apr 2008 03:00 GMT
Now that I am taking a second look at your problem, what about creating a
"partial class Settings"?
This is actually built in .Net, all you do is go to the settings designer
and click on the "View Code" toolbar button. This will automatically
generate the class for you (it can't get any easier)!
Then you can make the changes in the automatically generated class so that
it looks something like the following:
internal sealed partial class Settings
{
public Settings()
{
this.SettingsLoaded += this.Settings_SettingsLoaded;
}
private void Settings_SettingsLoaded(object sender,
System.Configuration.SettingsLoadedEventArgs e)
{
if(this.TextEditor == string.Empty)
this.TextEditor =
System.Environment.ExpandEnvironmentVariables(@"%windir%\NOTEPAD.EXE");
}
}
That's it, if the value of "TextEditor" is empty (this should be your
default value) then you set it to the proper value. After that, the value
should be hardcoded to the exact path.
Does this help you?
> Thanks, but what a pain! I thought this would be such an easy thing to
> do, but it doesn't seem to be the case. What would you suggest instead of
[quoted text clipped - 49 lines]
>>>
>>> Thanks for any help,
Clarks Computing - 30 Apr 2008 21:01 GMT
That's exactly what I was looking for! That did the trick. Thank you so
much.
> Now that I am taking a second look at your problem, what about creating a
> "partial class Settings"?
[quoted text clipped - 82 lines]
>>>>
>>>> Thanks for any help,
Ben Voigt [C++ MVP] - 29 Apr 2008 23:47 GMT
> Hi all,
>
[quoted text clipped - 8 lines]
> Is there any way to do this at design time, or what is the best way to
> handle this?
Make the default a special string ("__DEFAULT__" or maybe just ""), test for
it and substitute your dynamic value.
Settings can't be dynamic, they are stored in an xml file.
> Thanks for any help,
Clarks Computing - 30 Apr 2008 21:03 GMT
Yep, I get it now. I appreciate your response.
>> Hi all,
>>
[quoted text clipped - 15 lines]
>
>> Thanks for any help,