c#如何将字符串str保存到.txt文件里?
stringtext=reader.ReadToEnd();Console.WriteLine(text);FileStreamfs=newFileStream("f:/...
string text = reader.ReadToEnd();
Console.WriteLine(text);
FileStream fs = new FileStream("f:/中国山东/1.txt", FileMode.Open);
fs.Write(text,0,text.Length);看看哪里不对 展开
Console.WriteLine(text);
FileStream fs = new FileStream("f:/中国山东/1.txt", FileMode.Open);
fs.Write(text,0,text.Length);看看哪里不对 展开
展开全部
FileStream fs = new FileStream("f:/中国山东/1.txt", FileMode.Open,FileAccess.Write);试试?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/// <summary>
/// 检查是否存在文件夹
/// </summary>
public void change()
{
string path = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt";
if (!Directory.Exists(Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd")))
{
Directory.CreateDirectory(Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd"));
}
if (!File.Exists(path))
{
FileStream fs = File.Create(@"" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt");
fs.Close();
}
}
/// <summary>
/// 写入文本文件
/// </summary>
/// <param name="value"></param>
public void text(string value)
{
change();
string path = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt";
FileStream f = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(f);
sw.WriteLine(value);
sw.Flush();
sw.Close();
f.Close();
}
自己一直在用的。path改为 f:/中国山东/1.txt就可以了
/// 检查是否存在文件夹
/// </summary>
public void change()
{
string path = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt";
if (!Directory.Exists(Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd")))
{
Directory.CreateDirectory(Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd"));
}
if (!File.Exists(path))
{
FileStream fs = File.Create(@"" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt");
fs.Close();
}
}
/// <summary>
/// 写入文本文件
/// </summary>
/// <param name="value"></param>
public void text(string value)
{
change();
string path = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\TextMessage.txt";
FileStream f = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(f);
sw.WriteLine(value);
sw.Flush();
sw.Close();
f.Close();
}
自己一直在用的。path改为 f:/中国山东/1.txt就可以了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询