简体   繁体   English

如何在Windows窗体C#的资源​​文件夹中编辑文本文件

[英]How to edit Text file in Resources Folder of Window form c#

I am trying to Edit a text file which is consist in resources folder of window form project. 我正在尝试编辑一个文本文件,该文件包含在窗口表单项目的资源文件夹中。 C#. C#。

I am using this code but it is not writing over text file. 我正在使用此代码,但未覆盖文本文件。 No errors come from this 没有错误来自此

using (System.IO.StreamWriter file = new System.IO.StreamWriter(Namespace.Properties.Resources.textfile))
{
    for (int i = 0; i < res2.Count(); i++)
    {
        label3.Text = "Updating ... ";
        label3.Visible = true;
        label3.Refresh();

        file.WriteLine("asdasD");
    }
    file.Close();
}

As @LarsTech states in this answer , what you are trying to do is not recommended . 就像@LarsTech在此答案中指出的那样, 不建议您尝试执行操作。 Resource files are not meant to be written to--they should only be read from. 资源文件无意写入,只能从中读取。 If you want a place to put files, put them somewhere like Environment.SpecialFolders . 如果您想放置文件,请将其放置在Environment.SpecialFolders

You could also use the AppData folder on the user's computer, which is typically used for exactly what you are trying to achieve: 您还可以在用户计算机上使用AppData文件夹,该文件夹通常用于您要实现的目标:

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "textfile.txt");    
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName))
{
    //if the file doesn't exist, create it
    if (!File.Exists(fileName))
        File.Create(fileName);

    for (int i = 0; i < res2.Count(); i++)
    {
        label3.Text = "Updating ... ";
        label3.Visible = true;
        label3.Refresh();
        file.WriteLine("asdasD");
    }
}

As you can see, I removed the file.Close() since it isn't necessary if you are using the using block. 如您所见,我删除了file.Close()因为如果您正在使用using块,则没有必要。 You can do this if you are using a member which implements the IDisposable interface, which StreamWriter does. 如果使用的成员实现了StreamWriter实现的IDisposable接口,则可以执行此操作。

This should take care of everything for you. 这应该为您做好一切。 You won't have to create any files or worry about where they are. 您无需创建任何文件,也不必担心它们在哪里。

I am able to write and then read a file using your code in a console application. 我可以在控制台应用程序中使用您的代码编写然后读取文件。 Can you run this code (console application) and tell me if you have any exception ? 您可以运行此代码(控制台应用程序)并告诉我是否有任何异常吗?

    static void Main(string[] args)
    {

        string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "textfile.txt");
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName))
        {
            //if the file doesn't exist, create it
            if (!File.Exists(fileName))
                File.Create(fileName);

            for (int i = 0; i < 2; i++)
            {
                file.WriteLine("asdas2");
            }
        }

        using(System.IO.StreamReader fr = new StreamReader(fileName))
        {
            Console.WriteLine(fr.ReadToEnd());
        }
    }

Note, if you are trying to append to the existing file (write at the end of it and keep existing content), you need to use System.IO.StreamWriter(fileName, true). 请注意,如果尝试追加到现有文件(在文件末尾写入并保留现有内容),则需要使用System.IO.StreamWriter(fileName,true)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM