写入项目C#中存在的文件 [英] Write to a file that exist in the project C#

查看:83
本文介绍了写入项目C#中存在的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将列表中的信息写入CSV文件。这个文件已经在项目中,我试图从任何计算机写入该文件(因此它不是一直使用本地文件路径,即D // Projects .....)。



请注意:

我有UserControls文件夹。在其中一个UserControls中,我将保存到此文件夹之外的CSV。我的意思是这样的:



ProjectFolder => ProjectFolder =>此文件夹包含=>项目文件

项目解决方案课程

csv文件夹(UserControls)



我尝试过什么:



这真是令人沮丧,因为我可能正在做一些愚蠢的事情而且无法注意到这一点。我输入了完整的项目文件并正确保存到该csv,只是无法使其工作,所以它不需要完整的路径。以下是代码的样子:



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);
        }
    }
}





我尝试了几种方法来保存到AppendAllText中的csv文件:



@。\People.csv

@./ People.csv

@../ people.csv

@people.csv



以上都不是工作。



我知道该文件将显示在调试文件夹中。但我希望它保存在调试文件夹之外。就像我在上面的示例中所示。



任何帮助都会被评估。

谢谢。



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.

推荐答案

不要将数据文件存储在应用程序文件夹中:它在开发中工作正常,但在生产中失败,因为您的应用程序安装在Program Files文件夹下,并且对该文件的写访问权限是为了防止病毒活动。



请看这里:我应该在哪里存储我的数据? [ ^ ] - 它显示了更合适的地方!
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!


这篇关于写入项目C#中存在的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆