views:

61

answers:

4

Hi i am trying to make a simple application which will be used to point a web browser control to some of our web applications at my work. I would like to have only one exe file but also have an admin window to change some of the settings and have them persist when the application is closed. is that possible? i have looked at the application settings resources part but as i understand that makes a file that loads the settings.

I don't want to have to parse a file or have anything but ONE file so please don't suggest doing that if it is possible. (i don't mean to sound like a dick its just that is not at all what i want or can do)

+4  A: 

Just use application settings - that will create a single file, you won't have to do any parsing, it'll all be fine.

It'll be separate to the exe file, but unless you meant that "ONE file" to include the executable and rewrite that on the fly, it should fit your description easily.

For example, let's build a console app that just remembers how many times it's been launched:

  1. Create a new console application project
  2. Go to the properties page, and click into the Settings tab.
  3. Click on the link to create a settings file
  4. Type in the table to create a setting called "LaunchCount" of type int. Make it either user scope or application scope, depending on whether you want it to be persisted per user or system-wide.
  5. Hit Ctrl-S to save.
  6. In the Main method in Program.cs, write this code:

    Settings settings = Settings.Default;
    settings.LaunchCount++;
    Console.WriteLine("Launch count: {0}", settings.LaunchCount);
    settings.Save();
    
  7. Add the appropriate using directive for Settings (put the cursor in Settings and hit Ctrl-.)

  8. Run the app several times, and observe the number increasing.
Jon Skeet
no i meant only the executable file only. i am deploying this application to users desktops directly and to reduce the clutter and i just wanted to have one single thing to make it simple and easy. i would just like to have the ability to change the default url that is loaded and not have to make change to the actual application when i need to change it. this is kind of a build once and never have to fix bugs application cause it is literally just a web browser and 3 buttons kinda thing.
cferbs
@cferbs: It's really not clear who's going to be changing things here. Would you be changing the URL and rebuilding the exe, then redistributing it? Or would each user run this "admin panel"? Note that with the approach I've described above, the users never have to deal with the settings file themselves. It's simple and easy, both for the user and the developer. Where do you see things being complicated or hard? Rewriting the exe on the fly is what *I'd* consider complicated...
Jon Skeet
"Build once and never have to fix bugs". Hah. Hahah.
Matti Virkkunen
the user would change run the admin panel and change the default URL if we change the server that the web application runs on. we are doing this with little it involvement so no dns help, just ip address and they will be running this on a terminal server where we can't install stuff so i just wanted to have only the exe and if we needed to make changes not have to redeploy the exe just have the manager change the url. but if i have to use the built in settings thing with the file than thats ok i guess.
cferbs
@cferbs: Well, the built-in approach seems to satisfy both simple and easy, so I'd go for that... at least *try* it in your environment, which will take about 5 minutes.
Jon Skeet
i'll try it. Thanks everyone. i figured that would be the way to do it just checking if there was another way.
cferbs
A: 

You can't have persisted settings without having a separate file...safely. You must either have a separate file, which is the standard and suggested approach approach, like the one created with Application settings, or you must use something like the registry to save settings.

Keep in mind, though, that using the registry is highly discouraged due to security reasons. Plus most companies don't allow access to registries anyway which means that anyone without this access could not use the settings feature.

Mike Webb
A: 

There are several ways to do this. You can use a command-line argument to do that. Launch the app from the shell and put in your command line argument and change how it launches.

A UNIX-y approach is to look at the name of the exe and change behavior based on that. If I recall correctly, rsh and rlogin are the same executable - they just look at argv[0] to decide how to run. In windows, this is straight forward - look at System.Environment.GetCommandLineArgs - if there is a non-empty string in the 0th element of that, it will be your executable name.

For persisting settings, see Jon Skeet's answer.

plinth
A: 

I have to say that this is generally a bad idea, but I've done this before a long time ago in VB6. I created a Resource within the exe and then (somehow) directly manipulated it.

The problem is, is that this is usually not possible within the .NET framework due to it being memory resident. These guys tryed it out in .net and they ended up creating an program in IL to do the heavy lifting... http://stackoverflow.com/questions/2742583/modify-emdeded-string-in-c-compiled-exe

Go with a settings file as Jon suggested!

Tom