C# 如何使用streamwriter/textwriter/file写入文件?
当程序重新启动时,我能够写入文件,因为它总是第一次尝试写入,但在同一执行过程中,它只在第一次工作,然后它抛出一个异常,说明进程无法访问文件,因为它正被另一个进程使用 我试图将这两种类型的代码写入文件。 我也用using语句尝试了上面的代码,但没有成功 写入文件后,我将其附加到邮件中,使用smtp客户端发送邮件C# 如何使用streamwriter/textwriter/file写入文件?,c#,filestream,streamwriter,textwriter,C#,Filestream,Streamwriter,Textwriter,当程序重新启动时,我能够写入文件,因为它总是第一次尝试写入,但在同一执行过程中,它只在第一次工作,然后它抛出一个异常,说明进程无法访问文件,因为它正被另一个进程使用 我试图将这两种类型的代码写入文件。 我也用using语句尝试了上面的代码,但没有成功 写入文件后,我将其附加到邮件中,使用smtp客户端发送邮件 var mail = new MailMessage(sender.Trim(), sender.Trim()); mail.Attachments.Add(new Attachment(
var mail = new MailMessage(sender.Trim(), sender.Trim());
mail.Attachments.Add(new Attachment(attachment));
mail.Body = body;
client.Send(mail);
client.Dispose();
邮件部分工作正常。尝试此解决方案,可能会有所帮助
using (StreamWriter stream = new StreamWriter(attachment, false))
{
stream.WriteLine("some text here");
}
试试这个解决方案,可能会有帮助
using (StreamWriter stream = new StreamWriter(attachment, false))
{
stream.WriteLine("some text here");
}
问题在于MailMessage实例,它使文件保持打开状态。处理邮件消息实例对我有效
mail.Dispose();
问题在于MailMessage实例,它使文件保持打开状态。处理邮件消息实例对我有效
mail.Dispose();
为了避免锁定问题,您可以像对象或ReaderWriter一样使用锁定: 此外,您可以使用File.AppendAllText或 附加所有行
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();
}
}
为了避免锁定问题,您可以像对象或ReaderWriter一样使用锁定: 此外,您可以使用File.AppendAllText或 附加所有行
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();
}
}
使用此方法:
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();
}
使用此方法:
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();
}
试试这个
using (StreamWriter str = new StreamWriter(attachment, false))
{
str.WriteLine("Heyy!! How are you");
}
试试这个
using (StreamWriter str = new StreamWriter(attachment, false))
{
str.WriteLine("Heyy!! How are you");
}
使用电子邮件附件不需要写入磁盘上的文件,例如,您可以使用MemoryStream。不要明确地进行处理,而是使用以下帮助:usingStreamWriter streamWriter=new StreamWriterattachment,false{streamWriter.Writequery;}尝试删除流和读写器:file.writealTextMyFileName,query;使用电子邮件附件不需要写入磁盘上的文件,例如,您可以使用MemoryStream。不要明确地进行处理,而是使用以下帮助:usingStreamWriter streamWriter=new StreamWriterattachment,false{streamWriter.Writequery;}尝试删除流和读写器:file.writealTextMyFileName,query;我相信你已经发现了问题所在。我建议使用using关键字而不是显式调用来处理。看看原因。我相信你已经发现了问题所在。我建议使用using关键字而不是显式调用来处理。原因见。