Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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.
Posted
Updated 3-Dec-17 6:28am
v2

1 solution

Don't store data files in the application folder: it works fine in development, but fails in production, because your app is installed under the "Program Files" folder, and write access to that is restricted to prevent virus activity.

Have a look here: Where should I store my data?[^] - it shows much more suitable places!
 
Share this answer
 
Comments
fellanmorgh 3-Dec-17 13:19pm    
Thanks for the tips. Will take a look at those

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