Mon Dec 12 2005
A few people asked for the source code/a download for the little widget I
whipped up on Friday, so I spent this morning adding a little bit of
persistence. The original code for switching my networks is still in there (and
still works on my machine if I change the code to point there) but I made it
more general and also used it as an excuse to explore the Settings API as well
as use a couple of neat features of VS 2005. (If you don't care about any of
that stuff and just want to see if you can build the project and make some use
out of it, it can be found here on
projectdistributor. )
So the settings goop in VS 2005 is pretty neat, but there's a limited subset
(it seems) that you can specify as types in the designer. I wanted to use a
Domain Object, tho - so, no designer love for me. I ended up making a simple
string setting just to see what code it generated for UserScopedSetting
properties. I ended up with this:
[global::System.Configuration.UserScopedSetting] public ConfigItem Disconnected { get { if (this["Disconnected"] == null) { ConfigItem item = new ConfigItem(); item.IPAddress = "127.0.0.1"; item.Condition = ConditionKind.Equals; item.DisplayText = "disconnected."; item.Icon = IconKind.Disconnected; this["Disconnected"] = item; } return (ConfigItem)this["Disconnected"]; } set { this["Disconnected"] = value; } }
I found that a suitable default value was needed to start off with in
settings, so I did something similar for a Home, Work, Unknown, and Other
network. This way I can persist the Settings using the Settings API instead of
something home cooked (although I'm not sure if you would have to to avoid
version conflicts...) UserScopedSettings look to me a lot like IsolatedStorage,
that is the version and the product name help determine where settings should
go. If you wanted to use the same settings across different versions, I guess
you would have to copy the user.config file from one version location to the
next.
Next I wanted to see what the designer had to offer. The table layout panel
was real nice. It took care of all of my layout issues with respect to space. To
get what I wanted done, I first created a dummy Collection that inherits from
System.ComponentModel.BindingList<>:
internal class ConfigItemList : BindingList<ConfigItem> {}
This is how I was able to add it as an Object datasource to the Data Sources
window:
From there, you can change the type of control you would like to output. I
simply Dock.Fill 'ed the TableLayoutPanel and added the correct amount of rows.
To get the labels to line up with the controls, I set all of their AnchorStyles
to Top | Left (that's Top AND Left). When each usercontrol is loaded up, I
manually bind to the information. When the Save button is clicked, all of the
info gets put back into the Settings object from the controls and
Settings.Default.Save() is called. This automatically saves the changed data to
a safe location under Documents And Settings (specifically Local
SettingsApplication DataCompanyNameFileName+ a bunch of other
stuffProductVersion).
Now, for the main reason I created this thing: I host a few external
sites at work, and I can't see them without an entry in my hosts file
(%windir%system32driversetchosts) for each one. At home (or anywhere else)
however, I need to be able to rely on DNS to be able to get me to those sites,
so I created 2 extra hosts files, hosts.work and hosts.home. hosts.home pretty
much just has the single entry:
127.0.0.1 localhost
whereas hosts.work contains definitions to the internal network address of
the servers involved. I created 2 batch files that copy over the hosts file
depending on where I am. The one to switch to work has the following
command:
copy /Y %windir%system32driversetchosts.work
%windir%system32driversetchosts
and the one for home simply replaces hosts.home into the hosts file the same
way. So the command to execute for work is the fully qualified path to the bat
file. Everything else has the fully qualified path to the switch to home batch
script. So now, I just have this small utility run on startup and stay in the
tray, and whenever the network is changed, I am automatically configured to go
with this utility.
[ Currently Playing : Quarantined - At the Drive-In -
Relationship of Command (5:24) ]