app.config

K

Khodr

Hello,
I am using VS.NET 2003 and vb. I build my application MyApp and it generates
MyApp.exe.config. So now MyApp.exe reads parameters from MyApp.exe.config.
Great and no problem!

I need to run the same program but with different configuration data. So I
made a copy of MyApp.exe and MyApp.exe.config to put them in another folder
and renamed the copy to MyApp2.exe and MyApp2.exe.config respectively. I ran
it but it did not read from MyApp2.exe.config. I also renamed
MyApp2.exe.config back to MyApp.exe.config and also did not work. (N.B. I
need to have MyApp.exe and MyApp2.exe with different names).

The question is: How can I make a copy of my program and its config file to
another folder, and run both programs MyApp.exe and MyApp2.exe and each read
from its own config file?

Any help will be highly appreciated.

Thanks

Khodr
 
C

Chris Dunaway

If they're in different folders, why do you have to rename them? Just
leave them with their original names.
 
K

Khodr

Thanks Chris for ur reply. As I mentioned, I need to have them with
differnet names. They both stay running and if I need to kill one of them
using Task Manager, the only way to know which one to kill is by the name.
Again, is there a way to make them run both "with different names" at the
same time. Thanks...
 
T

Tome

You can strip the path information and .exe extension from Application.ExecutablePath with a regular
expression. Then use that string to create the name of your config file that you create and access.

Regex reg = new Regex(@"(\w+)\.exe$");
string path = Application.ExecutablePath;

string configname = reg.Matches(path)[0].Groups[1].Value + ".config";

This way if the executable is test1.exe it will create a config file called test1.config.

---Tome
http://www.pcdotcom.com
 
K

Khodr

Yes I can compile "again" with different name. Just I was trying to see if
it can be easily done by simply copying and renaming. Why is it so
complicated to have 2 copies of the same program running with different
config files? What if I need to run 3 or more copies of the same program
with different config files?

Again, thanks for your reply.
 
B

Brian Gideon

Tome,

Here's a simple one line solution to get the config file name.

string file = Assembly.GetEntryAssembly().Location + ".config";

Brian
 
T

Tome

Brian,

Assembly.GetEntryAssembly().Location
Application.ExecutablePath

Both give you the same results. You could indeed use the full path and just add .config. That
would make test1.exe use a config file called test1.exe.config

you could also use the full path and just replace the .exe with .config

string configname = Application.ExecutablePath.Replace(".exe",".config");
or
string configname = Assembly.GetEntryAssembly().Location.Replace(".exe",".config");


---Tome
http://www.pcdotcom.com
 
K

Khodr

Hi Guys,

Thanks for all your answers. After all this discussion: Is there a solution
to my situtation "without" modifying my application and "without"
recompiling?

Simply, I want to run my program in more than one folder at the same time,
but with different content of config file. The reason I need this, is that
one version is pointing to Production and the other to Test.

Thanks again

Khodr
Tome said:
Brian,

Assembly.GetEntryAssembly().Location
Application.ExecutablePath

Both give you the same results. You could indeed use the full path and just add .config. That
would make test1.exe use a config file called test1.exe.config

you could also use the full path and just replace the .exe with .config

string configname = Application.ExecutablePath.Replace(".exe",".config");
or
string configname = Assembly.GetEntryAssembly().Location.Replace(".exe",".config");
 
T

Tome

Khodr,

After re-reading your initial post. You shouldn't have a problem at all. It all depends on how you
are loading your config file within your code.

Can you please post a snippit showing how you load the config file?

---Tome
http://www.pcdotcom.com


Hi Guys,

Thanks for all your answers. After all this discussion: Is there a solution
to my situtation "without" modifying my application and "without"
recompiling?

Simply, I want to run my program in more than one folder at the same time,
but with different content of config file. The reason I need this, is that
one version is pointing to Production and the other to Test.

Thanks again

Khodr
 
K

Khodr

Hi Tome,

That's how I get info from the config file:
System.Configuration.ConfigurationSettings.AppSettings.Get(key)

and it works very well with my original app, MyApp.exe with MyApp.exe.config

Like I mentioned before, the problem is when I make a copy of this program
in another folder and rename the copy to MyApp2.exe. Then it does not work.

Thanks


Tome said:
Khodr,

After re-reading your initial post. You shouldn't have a problem at all. It all depends on how you
are loading your config file within your code.

Can you please post a snippit showing how you load the config file?

---Tome
http://www.pcdotcom.com
 
T

Tome

OKay... now I see what you're doing. You are using a System.Configuration .config file, not your
own. This should work without a problem. I've just tested it with VS2005 and c# and it worked
without a hitch...

I compiled the program as scrap.exe
Created a new folder.
Copied the program and the config file into the new folder.
Renamed the executable to scrap2.exe
Renamed the config file to scrap2.exe.config
Modified the contents of scrap2.exe.config to have different data.

Ran both programs simultaneously and received the proper results in each. It works fine for me. If
you don't rename the config file, it will not work. It will populate the values with either "" or
null value (unsure which one).

I'm not sure why it's not working for you. What values is your program getting from your config
file? Are they blank strings, or are they getting the wrong strings?

---Tome
http://www.pcdotcom.com
 
K

Khodr

Tome, thanks again. This is really bugging me. I did the same same thing
like you did. It does not work... I will try to put some debug info to see
why not it's not working....
 
K

Khodr

Hi Tome and Hi to all,

Finally, I resolved this puzzle. You won't believe it.

What I did that caused all the trouble:
MyApp.exe is in a folder called: "C:\MyFolder\"
1) so I copied it along with MyApp.exe.config to "C:\MyFolder (TEST)\"
2) renamed MyApp.exe to MyApp-TEST.exe
3) renamed MyApp.exe.config to MyApp-TEST.exe.config
Result: FAILURE

SOLUTION:
I renamed the "folder" itself "C:\MyFolder (TEST)\" to "C:\MyFolder-TEST\"
Now it works. Can you believe it? It did not like the "(" and ")" in the
folder name itself.

Thank you all...

Khodr


Khodr said:
Tome, thanks again. This is really bugging me. I did the same same thing
like you did. It does not work... I will try to put some debug info to see
why not it's not working....



Tome said:
OKay... now I see what you're doing. You are using a
System.Configuration
.config file, not your
own. This should work without a problem. I've just tested it with
VS2005
and c# and it worked
without a hitch...

I compiled the program as scrap.exe
Created a new folder.
Copied the program and the config file into the new folder.
Renamed the executable to scrap2.exe
Renamed the config file to scrap2.exe.config
Modified the contents of scrap2.exe.config to have different data.

Ran both programs simultaneously and received the proper results in
each.
It works fine for me. If
you don't rename the config file, it will not work. It will populate
the
values with either "" or
null value (unsure which one).

I'm not sure why it's not working for you. What values is your program getting from your config
file? Are they blank strings, or are they getting the wrong strings?

---Tome
http://www.pcdotcom.com

Hi Tome,

That's how I get info from the config file:
System.Configuration.ConfigurationSettings.AppSettings.Get(key)

and it works very well with my original app, MyApp.exe with MyApp.exe.config

Like I mentioned before, the problem is when I make a copy of this program
in another folder and rename the copy to MyApp2.exe. Then it does not work.

Thanks


Khodr,

After re-reading your initial post. You shouldn't have a problem at all.
It all depends on how you
are loading your config file within your code.

Can you please post a snippit showing how you load the config file?

---Tome
http://www.pcdotcom.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top