Read data from app.config and use it across multiple classes in c#
Hi I am reading the values from app.config. I have different sections as below.
<TEST>
<add key="key" value="value1" />
</TEST>
<DEV>
<add key="key" value="value1" />
</DEV>
I am accessing these values in my class files as below.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;
If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.
c# .net app-config
add a comment |
Hi I am reading the values from app.config. I have different sections as below.
<TEST>
<add key="key" value="value1" />
</TEST>
<DEV>
<add key="key" value="value1" />
</DEV>
I am accessing these values in my class files as below.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;
If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.
c# .net app-config
add a comment |
Hi I am reading the values from app.config. I have different sections as below.
<TEST>
<add key="key" value="value1" />
</TEST>
<DEV>
<add key="key" value="value1" />
</DEV>
I am accessing these values in my class files as below.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;
If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.
c# .net app-config
Hi I am reading the values from app.config. I have different sections as below.
<TEST>
<add key="key" value="value1" />
</TEST>
<DEV>
<add key="key" value="value1" />
</DEV>
I am accessing these values in my class files as below.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection("DEV") as NameValueCollection;
If I want to read values from 5 different classes then I have to write above code in all 5 classes and If I want to change DEV to TEST then I need to change in 5 places. Is there any way I can simplify this steps? Or Is there any best practices I can follow up? Any help would be greatly appreciated. Thank you.
c# .net app-config
c# .net app-config
edited Nov 23 '18 at 13:35
CodeConstruct
175113
175113
asked Nov 23 '18 at 9:19
NiranjanNiranjan
20919
20919
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Access Modifiers
make a public static string containing the value you want.
Then just refer to that instead of directly inserting the value.
And whenever you want to change it, you just have to change the value of the static in that one place.
public static string Section = "DEV";
and then you can just insert this, so you only have to change the value of Section when you want to switch.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;
Okay thanks. In which file I need to public?
– Niranjan
Nov 23 '18 at 9:36
any file as long as it's in the same assembly. more info on the link added :)
– Dennis Vanhout
Nov 23 '18 at 9:40
@Niranjan if it worked, please mark as answer, if not, feel free to ask :)
– Dennis Vanhout
Nov 23 '18 at 9:47
1
Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.
– User_CO
Nov 23 '18 at 9:50
add a comment |
Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443738%2fread-data-from-app-config-and-use-it-across-multiple-classes-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Access Modifiers
make a public static string containing the value you want.
Then just refer to that instead of directly inserting the value.
And whenever you want to change it, you just have to change the value of the static in that one place.
public static string Section = "DEV";
and then you can just insert this, so you only have to change the value of Section when you want to switch.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;
Okay thanks. In which file I need to public?
– Niranjan
Nov 23 '18 at 9:36
any file as long as it's in the same assembly. more info on the link added :)
– Dennis Vanhout
Nov 23 '18 at 9:40
@Niranjan if it worked, please mark as answer, if not, feel free to ask :)
– Dennis Vanhout
Nov 23 '18 at 9:47
1
Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.
– User_CO
Nov 23 '18 at 9:50
add a comment |
Access Modifiers
make a public static string containing the value you want.
Then just refer to that instead of directly inserting the value.
And whenever you want to change it, you just have to change the value of the static in that one place.
public static string Section = "DEV";
and then you can just insert this, so you only have to change the value of Section when you want to switch.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;
Okay thanks. In which file I need to public?
– Niranjan
Nov 23 '18 at 9:36
any file as long as it's in the same assembly. more info on the link added :)
– Dennis Vanhout
Nov 23 '18 at 9:40
@Niranjan if it worked, please mark as answer, if not, feel free to ask :)
– Dennis Vanhout
Nov 23 '18 at 9:47
1
Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.
– User_CO
Nov 23 '18 at 9:50
add a comment |
Access Modifiers
make a public static string containing the value you want.
Then just refer to that instead of directly inserting the value.
And whenever you want to change it, you just have to change the value of the static in that one place.
public static string Section = "DEV";
and then you can just insert this, so you only have to change the value of Section when you want to switch.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;
Access Modifiers
make a public static string containing the value you want.
Then just refer to that instead of directly inserting the value.
And whenever you want to change it, you just have to change the value of the static in that one place.
public static string Section = "DEV";
and then you can just insert this, so you only have to change the value of Section when you want to switch.
protected static NameValueCollection keyvalue = ConfigurationManager.GetSection(Section) as NameValueCollection;
edited Nov 23 '18 at 9:38
answered Nov 23 '18 at 9:34
Dennis VanhoutDennis Vanhout
917
917
Okay thanks. In which file I need to public?
– Niranjan
Nov 23 '18 at 9:36
any file as long as it's in the same assembly. more info on the link added :)
– Dennis Vanhout
Nov 23 '18 at 9:40
@Niranjan if it worked, please mark as answer, if not, feel free to ask :)
– Dennis Vanhout
Nov 23 '18 at 9:47
1
Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.
– User_CO
Nov 23 '18 at 9:50
add a comment |
Okay thanks. In which file I need to public?
– Niranjan
Nov 23 '18 at 9:36
any file as long as it's in the same assembly. more info on the link added :)
– Dennis Vanhout
Nov 23 '18 at 9:40
@Niranjan if it worked, please mark as answer, if not, feel free to ask :)
– Dennis Vanhout
Nov 23 '18 at 9:47
1
Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.
– User_CO
Nov 23 '18 at 9:50
Okay thanks. In which file I need to public?
– Niranjan
Nov 23 '18 at 9:36
Okay thanks. In which file I need to public?
– Niranjan
Nov 23 '18 at 9:36
any file as long as it's in the same assembly. more info on the link added :)
– Dennis Vanhout
Nov 23 '18 at 9:40
any file as long as it's in the same assembly. more info on the link added :)
– Dennis Vanhout
Nov 23 '18 at 9:40
@Niranjan if it worked, please mark as answer, if not, feel free to ask :)
– Dennis Vanhout
Nov 23 '18 at 9:47
@Niranjan if it worked, please mark as answer, if not, feel free to ask :)
– Dennis Vanhout
Nov 23 '18 at 9:47
1
1
Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.
– User_CO
Nov 23 '18 at 9:50
Better to keep the string "DEV" in the app.config and then call the string as you are doing for the key and value. This helps in not building the console when you change the string.
– User_CO
Nov 23 '18 at 9:50
add a comment |
Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode
add a comment |
Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode
add a comment |
Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode
Hi I would suggest following this link which tells how to manage debug and release mode in C#.C# Debug & Release mode
answered Nov 23 '18 at 10:08
CodeConstructCodeConstruct
175113
175113
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443738%2fread-data-from-app-config-and-use-it-across-multiple-classes-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown