C# 创建新RTF文件
这个和WINDOWS创建RTF文件一样
public void CreateRtfFile(string RtfFileName) { RichTextBox richTextBox1 = new RichTextBox(); richTextBox1.SaveFile(RtfFileName); }
private void button1_Click(object sender, EventArgs e) { string Dir = @"C:\Users\Admin\Desktop\"; string FileTitle = "新建 RTF 文档"; string FileExt = ".rtf"; string s1 = Dir + FileTitle + FileExt; int j = 2; if (System.IO.File.Exists(s1)) { string s = String.Format("{0}{1}({2}){3}",Dir,FileTitle,j,FileExt); while (System.IO.File.Exists(s)) { j++; s = String.Format("{0}{1}({2}){3}", Dir, FileTitle, j, FileExt); } if (!System.IO.File.Exists(s)) { CreateRtfFile(s); } } else CreateRtfFile(s1); }
创建一个文件并返回这个文件名称
/// <summary> /// 新建 RTF 文档 新建 RTF 文档(2) 新建 RTF 文档(3)... /// string s = CreateRtfFile2(@"C:\Users\Admin\Desktop\"); /// richTextBox1.AppendText(s + "\r\n"); /// </summary> /// <param name="path"></param> /// <returns>返回当前创建的文件名 如 “新建 RTF 文档” </returns> public string CreateRtfFile2(string path) { //string path = @"C:\Users\Admin\Desktop\"; string FileTitle = "新建 RTF 文档"; string FileExt = ".rtf"; string Result = FileTitle; string s1 = path + FileTitle + FileExt; int j = 2; if (System.IO.File.Exists(s1)) { string s = String.Format("{0}{1}({2}){3}", path, FileTitle, j, FileExt); while (System.IO.File.Exists(s)) { j++; s = String.Format("{0}{1}({2}){3}", path, FileTitle, j, FileExt); } if (!System.IO.File.Exists(s)) { CreateRtfFile(s); FileTitle = System.IO.Path.GetFileNameWithoutExtension(s); } } else CreateRtfFile(s1); return FileTitle; }
使用
private void button1_Click(object sender, EventArgs e) { string s = CreateRtfFile2(@"C:\Users\Admin\Desktop\"); richTextBox1.AppendText(s + "\r\n"); }