Hi, I am trying to write information from a List to a CSV file. This file is already in the project and I was trying to write to that file from any computer (so it is not using the local file path all the time i.e. D//Projects.....).
Please note:
I have UserControls folder. In one of those UserControls I am saving to CSV that is outside this folder. What I mean by that is something like that:
ProjectFolder => ProjectFolder => This Folder Contains => Project File
Project Solution Classes
csv File Folder(UserControls)
What I have tried:
This is really frustrating because i'm probably doing something silly and can't notice that. I have typed the full project file and it correctly save to that csv, just can't get it working so it does not need the full path. Here is how the code looks like:
private void SaveToCsvFile()
{
StringBuilder content = new StringBuilder();
Person person = people.First();
foreach (var person in people)
{
try
{
content.AppendLine("Name, Surname, Age");
content.AppendLine(policy.Name + "," +
policy.Surname + "," +
policy.Age);
File.AppendAllText(@"People.csv");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Something went wrong",
MessageBoxButtons.OK);
}
}
}
I have tried several ways to save to the csv file in the AppendAllText:
@".\People.csv""
@"./People.csv"
@"../people.csv"
@"people.csv"
None of the above worked.
I am aware that the file will display in the debug folder. But I would like it to save outside the debug folder. Like I showed in the above example.
Any help would be apprciated.
Thanks.