C#如何将字符串保存为文件?

2024-11-27 02:38:01
推荐回答(4个)
回答1:

string filePath = HttpContext.Current.Server.MapPath(htmlpath);
using(StreamWriter sw = new StreamWriter(filePath)){
sw.Write(ContentHtml);
}
这档模样就ok了,注意,你的悉茄这个htmlpath必行陆缓须以你的htm扩展名结尾

回答2:

using System.IO;//命名空间

string strPath=@"D:\abcdefg\aaa\a.html";
string value="abceTest<尘让核/派掘html>滑蚂";

if (!Directory.Exists(Path.GetDirectoryName(strPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(strPath));
}

File.AppendAllText(strPath, value,Encoding.Default);

回答3:

VS2008中,带者绝保存祥芦和打开文件对话框 ,我工程中的源谨嫌带代码
//将命令窗口的内容保存到文件
private void btnSave_Click(object sender, EventArgs e)
{
StreamWriter sw;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = Properties.Resources.FileType;
sfd.ShowDialog();
if (sfd.FileName != null)
{
try
{
sw = new StreamWriter(sfd.FileName);
}
catch
{
return;
}
sw.WriteLine(rtbCommand.Text);
sw.Close();
}
}

//文件内容加载到命令窗口
private void btnOpen_Click(object sender, EventArgs e)
{
StreamReader sr;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = Properties.Resources.FileType;
ofd.ShowDialog();
if (ofd.FileName != null)
{
try
{
sr = new StreamReader(ofd.FileName);
}
catch
{
MessageBox.Show(Properties.Resources.OpenfileError);
return;
}
rtbCommand.Text += sr.ReadToEnd();
sr.Close();
}
}

回答4:

文件写入