Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So im trying to figure out for alot of hours how i can make it save my listbox items in a existing file.

So basically what my program does is ; whenever i click the start button it creates a folder name with date... inside the folder it creates 2 text files "good" & "bad"..

So whenever something is good it should put it in the listbox1. when i press a button i want to it save it automatically to the text file.

// create folder
           string myDate = DateTime.Today.ToString(" [yyyy-MM-dd]");
           DateTime now1 = new DateTime();
           now1 = DateTime.Today;
           string today = now1.ToString();
           Directory.CreateDirectory("Results" + myDate);
           File.WriteAllText(Path.Combine("Results" + myDate, "Good.txt"), "");
           File.WriteAllText(Path.Combine("Results" + myDate, "Bad.txt"), "");



save button (empty atm because nothing worked)
private void button9_Click(object sender, EventArgs e)
        {

        }


What I have tried:

i tried streamwriter but everytime it denies the folder or it cant find, and i dont really know how to implement a folder with date inside the code to make it find.

in my "checking" script at the end of "working" script it will perform automatically a click on the button9 to make it save after each working proxy.

This is basically the idea.
Posted
Updated 6-Jan-21 5:11am
Comments
Ersin Aydogdu 6-Jan-21 9:35am    
File.WriteAllText(Path.Combine("Results" + myDate, "Bad.txt"), "");

i have left ""blanc aswell because it didnt work neither to put listbox1.text in there like :

File.WriteAllText(Path.Combine("Results" + myDate, "Bad.txt"), "listBox1.Text");
CHill60 6-Jan-21 10:04am    
Format the date in the folder or filename in ISO 8601 format[^] e.g. 20210106.
If you are using "normal" date format like 06/01/2021 the "/" is an illegal character in a file path
Sorry - just realised you are doing that.
You need to tell us exactly what the error message is - but you are including the filename in the Path.Combine ... I don't think that is right
Richard MacCutchan 6-Jan-21 10:09am    
You just need to iterate the items in the ListBox and write each one to whichever file it belongs to.
CHill60 6-Jan-21 10:10am    
Ah - is it because the documentation says "The parameters are not parsed if they have white space." and your myDate variable begins with a space? Try taking the space off and see if that works
string myDate = DateTime.Today.ToString("[yyyy-MM-dd]");
jsc42 6-Jan-21 10:15am    
Try changing your
File.WriteAllText(Path.Combine("Results" + myDate, "Bad.txt"), "listBox1.Text");
to
File.WriteAllText(Path.Combine("Results" + myDate, "Bad.txt"), listBox1.Text);

i.e. getting the text from lstBox1 rather than writing the literal string "listBox1.Text"

1 solution

File.WriteAllText empties any existing file and starts it again - so it's probably File.AppendAllText you want to use instead.

But the problem is likely to be where you are storing the data - because you don't give any path information the folder you create will be in the current directory - which in production will be under Program Files and will be write protected for security.
If you specify a full path, then the folder will be created in a "sensible place" instead of under the EXE file, and it's very likely your problem will disappear.

Have a look here: Where should I store my data?[^] for some better ideas.
 
Share this answer
 

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