Click here to Skip to main content
15,171,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So basically what i have done is created a program, whever i click button "start" it creates a folder in the main folder. In that folder it creates 2 text files.

So whenever i click a button in this case button9 i want it to save it in that folder and overwrite text file #1.

So what it does now is, i found a way to make it save the text file in the main folder but not in the folder that is made after pressing start.

I have tried to fix it for like 2 days but can't find a solution.

Code for the button that saves it:

{                
                var saveFile = new SaveFileDialog();

                string path = System.IO.Directory.GetCurrentDirectory();
                saveFile.InitialDirectory = path;            
                saveFile.FileName = "Good";
                saveFile.Filter = "Text (*.txt)|.txt";

                using (var sw = new StreamWriter(saveFile.FileName + ".txt"))
                    foreach (var item in listBox1.Items)
                        sw.Write(item.ToString() + Environment.NewLine);
             
            }



The folder that is created after clicking start:
// 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.AppendAllText(Path.Combine("Results" + myDate, "Good.txt"), listBox1.Text);
            File.AppendAllText(Path.Combine("Results" + myDate, "Bad.txt"), listBox2.Text);


so the question is how do i make the txt file (from button9) go in that folder(from start button)?

What I have tried:

tried to find a way to make it find the folder, it is little sketchy for me because the name has date in it.
Posted
Updated 7-Jan-21 3:21am
v2

You have created a SaveFileDialog but you never use it. So when you use saveFile.FileName in your using statement the name will be just "Good". in fact there is no reason to have the dialog since you already have the directory and filename in your code. You just need to put them together with a call to Path.Combine Method (System.IO) | Microsoft Docs[^].

As an aside, do not use relative paths in your code, as you cannot be certain what is the current directory of the application. Start by choosing a good base path as described in: Where Should I Store My Data?[^].
   
To add to what Richard has said ...

As I told you yesterday (How to save listbox to existing text file with date[^]) this is a really bad idea that will fail in production because the whole EXE folder is security protected.

Go back there, read it again, and store you data in a safe and sensible place!
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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