Fill Listbox with System.IO.File.ReadAllLines and a vaiable Filename c#

118 Views Asked by At

im learning c# and started to Programm a tool for my own.

I fill some listboxes with a TextBox and save this listbox to a txt file. This works fine with: System.IO.File.WriteAllText

When i start the Programm again, the listboxes will fill direct from the txt file, this also works.

 string[] lines = System.IO.File.ReadAllLines("PWTool_Save.txt");

 foreach (string line in lines){.....}

But now i want to make the Savefiles dynamic. They have now the Name

string sPath = "PWTool_Save_"+user1+".txt";
System.IO.File.WriteAllText("PWTool_Save_" +user1+".txt", string.Empty);
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);

Save the file dynamic works fine, but load the file dynamic dont works.

Example:

I save the file with the Name

string user1 = "Tom"
"PWTool_Save_" +user1+".txt"

PWTool_Save_Tom.txt

Now when i try to load it with

 string[] lines = System.IO.File.ReadAllLines("PWTool_Save_"+user1+".txt");

it dont works, but when i manually say filename is

string[] lines = System.IO.File.ReadAllLines("PWTool_Save_Tom.txt");

it works :(

can someone help me? sorry for bad english

0

There are 0 best solutions below