【问题标题】:how to write file using streamwriter/textwriter/File?如何使用 streamwriter/textwriter/File 写入文件?
【发布时间】:2019-12-26 11:13:53
【问题描述】:

我可以在程序重新启动时写入文件(因为它总是第一次尝试写入)但是在相同的执行过程中它只在第一次工作然后它抛出一个异常声明 进程不能访问文件,因为它正被另一个进程使用

//1
StreamWriter streamWriter = new StreamWriter(attachment, false);
streamWriter.Write(query);
streamWriter.Dispose();
//2
TextWriter textWrtier = File.CreateText(attachment);
textWrtier.WriteLine(query);
textWrtier.Dispose();

我尝试将这两种类型的代码写入文件。 我也用 using 语句尝试了上面的代码,但是没有用。

写入文件后,我将其附加到邮件中(使用 smtp 客户端发送邮件)

var mail = new MailMessage(sender.Trim(), sender.Trim());
mail.Attachments.Add(new Attachment(attachment));
mail.Body = body;
client.Send(mail);
client.Dispose();

邮件部分工作正常。

【问题讨论】:

  • 您无需写入磁盘上的文件即可使用电子邮件附件 - 例如,您可以使用 MemoryStream
  • 不要明确地Dispose,而是在using的帮助下:using(StreamWriter streamWriter = new StreamWriter(attachment, false)) {streamWriter.Write(query);}
  • 尝试摆脱Streams和Reader/Writers:File.WriteAllText(myFileName, query);

标签: c# filestream streamwriter textwriter


【解决方案1】:

使用这个方法:

           private bool WriteToDisk(string content, string filePath)
            {
                using (FileStream sw = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    byte[] infos = Encoding.UTF8.GetBytes(content);
                    sw.Write(infos, 0, infos.Length);
                }

                return true;
            }

注意:如果您的文件不存在,请使用以下命令创建:

            private static void CreateFileIfNotExist(string filePath)
            {
                if (!File.Exists(filePath))
                {
                    var folderPath = Path.GetDirectoryName(filePath);
                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    File.Create(filePath).Dispose();
                }

【讨论】:

    【解决方案2】:

    试试这个解决方案,它可能会有所帮助

    using (StreamWriter stream = new StreamWriter(attachment, false))
            {
                stream.WriteLine("some text here");
            }
    

    【讨论】:

      【解决方案3】:

      问题出在 MailMessage 实例上,它使文件保持打开状态。处理邮件消息实例对我有用。

      mail.Dispose();
      

      【讨论】:

      【解决方案4】:
      string directory = Application.StartupPath + @"\Data Base";//create this folder
      string fileName = "write here file name" + ".dat";//you can change type of file like .txt
      string memoryPath = Path.Combine(directory, fileName);
      using (StreamWriter sw = File.CreateText(memoryPath))
      {
           sw.WriteLine("write here what you want");
      }
      

      【讨论】:

        【解决方案5】:

        试试这个

         using (StreamWriter str = new StreamWriter(attachment, false))
                    {
                        str.WriteLine("Heyy!! How are you");
                    }
        

        【讨论】:

          【解决方案6】:

          为避免锁定问题,您可以使用对象或 ReaderWriter 之类的锁定: 此外,您可以使用 File.AppendAllText 或代替 FileStream 和 StreamWriter 追加所有行。

          public static var lock = new ReaderWriterLock();
              public static void WriteToFile(string text)
              {
                  try
                  {
                      string fileName = "";
                      lock.AcquireWriterLock(int.MaxValue); 
          
                      File.AppendAllLines(fileName);
                  }
                  finally
                  {
                      lock.ReleaseWriterLock();
          
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 2016-05-03
            • 2014-06-22
            • 2020-05-23
            • 2014-02-22
            • 2018-08-24
            • 2011-10-29
            • 2011-08-29
            • 2016-10-21
            • 1970-01-01
            相关资源
            最近更新 更多