【问题标题】:C# - While writing into a text file, the ouput file is not created [duplicate]C# - 写入文本文件时,未创建输出文件[重复]
【发布时间】:2020-06-13 21:13:43
【问题描述】:

下面是我的代码。我的问题是根本没有创建 file.txt! 我找不到原因。程序应该创建它。你能帮帮我吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace WriteToFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = "Please help me to write something!!";

          System.IO.StreamWriter file = new System.IO.StreamWriter
(@"C:\Users\jgonc\source\repos\WriteToFile\WriteToFile\bin\Debug\file.txt");

            file.Flush();
            file.WriteLine(line);
            file.Close();

            Console.WriteLine("press a key");
            Console.ReadKey();


        }
    }
}

【问题讨论】:

  • 这能回答你的问题吗? Easiest way to read from and write to files 请检查msdn 以及
  • 您有任何异常(拒绝访问)吗?如果你不这样做,那么当你读回文件内容时,你没有使用该路径。无论如何,您都不应该对路径进行硬编码:部署应用程序时它不会存在。请注意,使用该构造函数会覆盖文件内容。 -- 从那里删除file.Flush();。

标签: c# file


【解决方案1】:
//line you want to write to the document
string line = "Please help me to write something!!";

//path to my documents
string docPath =
      Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

using (StreamWriter sw = new StreamWriter(Path.Combine(docPath, "test.txt")))
{
    sw.WriteLine(line); 
}

Console.WriteLine("press a key");
Console.ReadKey();

这里我使用 using 关键字。这非常有用,因为在参数中创建的任何对象都会在 using 段结束时自动销毁,这非常棒,因为这意味着您不需要用不必要的刷新和关闭来弄乱代码。

了解同花和平仓之间的区别也很重要。在这里,当使用段结束时,在 streamwriter 上调用 dispose,而后者又调用 close,从而关闭流。刷新只是清除buffer。

  • 检查您的应用程序是否有权写入我的文档目录 - here
  • 我更改了文档的路径,因为 bin 文件夹经常更改或被删除。

【讨论】:

    【解决方案2】:

    感谢您的支持和提示。 我已经调查了权限主题,似乎与它有关。 卸载我的防病毒软件后,它开始工作。

    下面是最终代码,使用 StreamWriter 编写几个字符串。

    static void Main(string[] args)
            {
                string[] lines = {
                            "Please help me to write something!",
                            "It's working now, it was related with my antivirus, it was somehow blocking  my permissions to the folder",
                            "Thanks for your support and your hints!!",
                };
    
                //string path = @"C:\Users\jgonc\source\repos\WriteToFile\WriteToFile\bin\Debug\file.txt";
                string path = @"file.txt";
    
                System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);  
                System.IO.StreamWriter file = new System.IO.StreamWriter(fs, System.Text.Encoding.ASCII);
    
                if (!File.Exists(path))
                {
                    Console.WriteLine("file was not created");
                }
                else
                {
    
                    using (file)
                    {
                        foreach (string line in lines) {
                            file.WriteLine(line);
                        }
                    }
                }
                fs.Close();
    
                Console.WriteLine("Press a Key to End...");
                Console.ReadKey();
    
    
            }
    

    【讨论】:

      【解决方案3】:

      试试:

      System.IO.StreamWriter file = new System.IO.StreamWriter
      (@"C:\Users\jgonc\source\repos\WriteToFile\WriteToFile\bin\Debug\file.txt", true);
      

      如果您想每次都创建文件,则附加属性设置为 false。

      【讨论】:

        猜你喜欢
        • 2018-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        • 2021-07-22
        • 1970-01-01
        • 2011-08-30
        • 2019-10-13
        相关资源
        最近更新 更多