|
There is no "App.Config" at runtime; it's xxx.exe.config.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Surely you mean <project name>.dll.config? (.net core and .net5, remember!)
Which is the one I have, obviously! VS didn't do any joke on me!
But, just to be sure I did rename it <project name>.exe.config, didnt run either
This seems to be very specific to single file deployment btw, I think you missed that.
|
|
|
|
|
In my case, the "build" creates it in the Debug and Release folders. One doesn't simply rename something.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Are you trolling?
If not, you are not helping.
Word of advice, get familiar with Visual Studio or MSBUILD. It does all the trivial renaming and such for you, there is no ambiguity except in your mind.
Also get familiar with .net core / 5.
it's .DLL.config.
UNLIKE .NET Framework 4 and before, which was .exe.config
modified 18-Nov-20 6:32am.
|
|
|
|
|
Got you.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I have a bunch of messages in a Constants file that I want to convert all string constants for messages displayed to users to be moved to a localized/globalization resource file.
I created a Resources.resx file and I'm trying to figure out how to reference a resource in a controller. For example this resource in my Resources.resx file
internal static string ExamItemResponseAddSuccessfully {
get {
return ResourceManager.GetString("ExamItemResponseAddSuccessfully", resourceCulture);
}
}
I want to reference it in a controller POST action to replace the bolded reference to the string constant in the method below. How do I reference the resource above in the method below.
public async Task<IActionResult> CreateDorItemResponse(CreateEditDorItemResponseViewModel dorToCreateItemResponse, CancellationToken cancellationToken)
{
var newDorItemResponse = new DorItemResponse();
if (ModelState.IsValid)
{
try
{
var item = await _dorService.GetItemAsync(dorToCreateItemResponse.ItemId, cancellationToken)
.ConfigureAwait(true);
_mapper.Map(dorToCreateItemResponse, newDorItemResponse);
newDorItemResponse.CreatedBy = User.Identity.Name;
newDorItemResponse.ModifiedBy = User.Identity.Name;
await _dorService.CreateEditItemResponse(newDorItemResponse, User.Identity.Name, cancellationToken).ConfigureAwait(true);
TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Constants.Dor.RESPONSE_ADD_SUCCESS, false));
return RedirectToAction(nameof(EditDorItem), new {id = item.ItemId, dorId = item.DorId});
}
catch (Exception)
{
TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Constants.Dor.RESPONSE_UPDATE_FAIL));
}
}
return View(dorToCreateItemResponse);
}
|
|
|
|
|
Something like this should work:
TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Properties.Resources.ExamItemResponseAddSuccessfully, false));
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Properties.Resources.ExamItemResponseAddSuccessfully, false));
I'm trying to replace the TempData.Put(TempDataKey...) stuff because I that is in the Constants file and I'm getting rid of that altogether.
I want to have the message come from the Resources file.
|
|
|
|
|
As far as I can see from your question, the values in the TempDataKey class represent the key of the TempData item. The message constants are defined in the Constants class, which is why I replaced that constant with the value from the resource file.
The TempData key will never be shown to the user.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello - I am constructing an URL to pass for API call.
It has a date value in it.
DateTime.Today.AddDays(0).ToString("yyyy-MM-dd")
Is there some way I can pass the (0) dynamically, reading from App.Config file?
I tried --
DateTime.Today.AddDays(ConfigurationManager.AppSettings["CompletedDays"].ToString("yyyy-MM-dd") which errored out and did not work. Any way to do this?
Thanks!
|
|
|
|
|
Member 14474607 wrote: which errored out and did not work. Sorry, there is no way we can guess what that means. Please edit your question and provide proper details of the problem.
|
|
|
|
|
var revisedDateTime = DateTime.Now.AddDays( (int) ConfigurationManager.AppSettings["CompletedDays"] );
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
The AppSettings indexer returns a string . The code will compile, but will throw an InvalidCastException at runtime.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
50-50 chance. There are some value-object pair settings out there (UWP: ApplicationDataContainer)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
|
If you want to get picky, AppSettings is based on a NameObjectCollectionBase.
Not my fault it's protected. But I won't whine about it.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
int completedDays;
int.TryParse(ConfigurationManager.AppSettings["CompletedDays"], out completedDays);
return DateTime.Today.AddDays(completedDays).ToString("yyyy-MM-dd"); Int32.TryParse Method (System) | Microsoft Docs[^]
If the setting isn't a valid integer, completedDays will be set to 0 . If you want to validate that the setting is valid, check the return value of TryParse .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
A much better way of doing things Richard - I often overlook TryParse and default to using one of the ConvertToxxx static methods
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
Hello
My first post on here, would really appreciate some help.
I'm programming in C# (Visual Studio .NET Forms).
My form has 9 labels and i'm trying to change the visibility properties of all the labels in a loop instead of changing them individually.
For example :
label1.Visibility = true;
label2.Visibility = true;
label3.Visibility = true;
label4.Visibility = true;
label5.Visibility = true;
label6.Visibility = true;
label7.Visibility = true;
label8.Visibility = true;
label9.Visibility = true;
Instead of using the above laborious method, use some kind of for loop instead to select the labels number to make the changes, is this possible?
Many thanks
Ron
|
|
|
|
|
A common trick to solve this type of problem is to loop over all of the controls in your form. If you are able to cast the control to a Label, set the visibility then. Don't forget to take into account that the code you write to loop over the controls has to take into account that some of the controls on your form might have children and there may be labels in those children.
|
|
|
|
|
Hi Pete
Thanks for your input.
That's what i'm trying to do, loop over all the controls but it's making the loop that i'm having difficulty with.
I see someone has just left me some example code so will check that out now, perhaps it's what i need.
Cheers
Ron
|
|
|
|
|
There are several ways: the messiest is to scan the Forms Controls collection:
foreach (Control c in Controls)
{
if (c is Label lab)
{
lab.Visible = true;
}
}
It's messy for two reasons:
1) If any of the labels are within a container (e.g. a Panel) then you need to recurse through all container Controls collection as well.
2) If there are any labels you didn't want to hide, you have to "tag" them somehow to prevent you affecting them.
A neater solution is to construct an array or List of the labels you want to work with, and use that instead of the Form Controls collection. It's a little work to set up, but it's more specific and easier to code.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
but to de-messy it :
he could check the Label-Name if it corresponds to those Labels he wants to work with.
|
|
|
|
|
That's nasty as well, particularly once he realizes that sticking to VS default names for everything is a really bad idea ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
LOL! the default names seem fine to me, no problems yet and been using it for years.
Every so often you need to do something unusual and this is one of those cases.
I'm soon to work on a program with 256 labels to display register values from a chip so need to create another loop program to run through and update all the values in both hex & binary.
If i change the label names to say result1, result2, result3.
Could i use an alternative loop method to select the label name seeing they have digits at the end?
|
|
|
|