Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm building a C# console application to get a list of all files that have been created or modified today in a specific folder. Call it folder "A" for now.

Using "StreamWriter", I want to create a list of those files and put the list in a text document. The name of the text document will be today's date.

How can I add this code to the App.Config file in C#?
"e:\\testdata\\" + dateStr + ".txt"


The "dateStr" is referencing a string value declared like this.

DateTime currentDateTime = DateTime.Now;
     string dateStr = currentDateTime.ToString("yyyy-MM-dd");


If I put this code on the main page, all works fine. But, when I try to call the file name and destination (in this case destination is "testdata" and file name is today's date) using "configuration manager" in the App.Config file, it creates the file and the data inside is correct, but it names the file "+dateStr+" instead of giving it today's date.

Any suggestions on how to get this in the App.Config file would be greatly appreciated.

What I have tried:

I tried using the \ escape method to escape the extra quote marks, but that didn't work.
I also tried removing some of the quote marks, and that didn't work.
I thought I could add the string "dateStr" to the App.Config page, but I can't get that to work either.

Here is the code I tried putting in my App.Config file:
<add key="rslt" value="e:\\testdata\\  + dateStr +  .txt"/>
Posted
Updated 11-Apr-19 7:44am

You shouldn't do that in app.config.

Try this:

C#
string filename = System.IO.Path.Combine(@"e:\testdata\", string.Format("{0}.txt", DateTime.Now.ToString("yyyyMMddhhmmss")));


or even this:

C#
string filename = string.Format(@"e:\testdata\{0}.txt", DateTime.Now.ToString("yyyyMMddhhmmss"));
 
Share this answer
 
v4
So why would this be a bad thing to put it in the App.Config file? (curious)

The only reason I wanted to put in the App.Config File is because I am going to do a batch build on the program and schedule it to run ever day.
I wanted to put this in the App.Config so if they changed the destination directory on me, I wouldn't have to rebuild, just modify the App.Config file with NotePad and save changes.

P.S. Thanks for the quick reply.
 
Share this answer
 
Comments
#realJSOP 11-Apr-19 14:14pm    
It's not that "it's bad", it's that it's "impossible". You simply can't put code into the config file and expect it to to run.

You can put the folder name, the extension, and even a format string into the app config, but you can't oput code in it.
That doesn't work because anything read from the config file is treated as literally a string;

<add key="rslt" value="e:\\testdata\\  + dateStr +  .txt"/>


There is nothing in that "string" that says dateStr should be replaced with the value of a variable you have called dateStr. How does .net know that's what you want to do?

A better way of handling this is to put a token in the string that is then replaced with your actual values in the code. So if you update the config to have

<add key="rslt" value="e:\\testdata\\{0}.txt"/>


In your code you can use string.Format to replace {0} with your variable contents

string filename = string.Format(textFromConfig, dateStr);


Format will replace {0} with the first param (dateStr), {1} with the second param (you don't have any) and so on.
 
Share this answer
 
Comments
Member 12969219 11-Apr-19 14:22pm    
Thanks for you help with this. I didn't think about using {0} in this situation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900