C# 如何将word文档保存在由Directory.CreateDirectory(docfile\u路径)创建的文件夹中?
此代码块引发一个名为“文件名无效”的错误 我想在这里面创建一个名为“test”的文件夹,还有一个名为“date”的文件夹,我想把word文档保存在这个date文件夹里,请帮助C# 如何将word文档保存在由Directory.CreateDirectory(docfile\u路径)创建的文件夹中?,c#,c#-4.0,office-interop,com-interop,C#,C# 4.0,Office Interop,Com Interop,此代码块引发一个名为“文件名无效”的错误 我想在这里面创建一个名为“test”的文件夹,还有一个名为“date”的文件夹,我想把word文档保存在这个date文件夹里,请帮助 public string File_path; public string docfile_path; public string filename; private void button1_Click(object sender, EventArgs e) { string time
public string File_path;
public string docfile_path;
public string filename;
private void button1_Click(object sender, EventArgs e)
{
string time = DateTime.Now.ToString("HH.mm.ss");
string date = DateTime.Today.ToShortDateString();
docfile_path = File_path+ "test" + date;
Directory.CreateDirectory(docfile_path);
filename = docfile_path + "worddoc"+"-" +".docx";
Word.Application app = new Word.Application();
Word.Document doc = new Word.Document();
try
{
doc = app.Documents.Open(filename);
}
catch
{
}
Word.Paragraph oPara1;
oPara1 = doc.Content.Paragraphs.Add();
oPara1.Range.Text = "Test Result";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;
oPara1.Range.InsertParagraphAfter();
oPara1.Range.InsertParagraphAfter();
Word.Paragraph oPara2;
oPara2 = doc.Content.Paragraphs.Add();
oPara2.Range.Text = "Test Name";
oPara2.Range.Font.Bold = 1;
oPara2.Format.SpaceAfter = 24;
oPara2.Range.InsertParagraphAfter();
doc.SaveAs2(filename);
doc.Close();
doc = null;
app.Quit();
app = null;
}
令人惊讶的是,这段代码可以编译并运行,但结果可能不是您想要的 此代码中有几处错误: 1.不能添加这样的字符串来创建路径,路径应在目录之间使用“/”符号创建。 这是一条合法途径:
string path = @"C:\Users\username\Desktop\Games";
这不是:
string path = @"C:UsersusernameDesktopGames";
您可以使用路径进行修复。组合函数如下所示:
docfile_path = Path.Combine(File_path , "test" , date);
确保所有路径字符串(包括上面代码中未显示值的文件路径)都使用此选项
2.你应该使用
Document doc = app.Documents.Add();
创建新Word文档而不是
Document doc = new Document();
3.您应该为字符串date使用不同的格式DateTime。ToSortDateString()将日期除以“/”符号,这将创建新文件夹。
尝试使用:
string date = DateTime.Today.ToString("dd.MM.yyyy");
4.我看不出有什么理由排队
doc = app.Documents.Open(filename);
您正在尝试打开要创建的文件
以下是我使用的代码:
string File_path = @"C:\Users\yakir\Desktop";
string docfile_path;
string filename;
string time = DateTime.Now.ToString("HH.mm.ss");
string date = DateTime.Today.ToString("dd.MM.yyyy");
docfile_path = Path.Combine(File_path , "test" , date);
Directory.CreateDirectory(docfile_path);
filename = Path.Combine(docfile_path, "worddoc" + "-" + ".docx");
Application app = new Application();
Document doc = app.Documents.Add();
Paragraph oPara1;
oPara1 = doc.Content.Paragraphs.Add();
oPara1.Range.Text = "Test Result";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;
oPara1.Range.InsertParagraphAfter();
oPara1.Range.InsertParagraphAfter();
Paragraph oPara2;
oPara2 = doc.Content.Paragraphs.Add();
oPara2.Range.Text = "Test Name";
oPara2.Range.Font.Bold = 1;
oPara2.Format.SpaceAfter = 24;
oPara2.Range.InsertParagraphAfter();
doc.SaveAs2(filename);
doc.Close();
doc = null;
app.Quit();
app = null;
}
使用调试器并查看变量。在您识别出错误后,静态类
Path
及其方法应该能够帮助您修复它。我认为您的问题在于目录和文件名之间可能缺少/
。尝试var fileName=Path.Combine(docfile\u Path,docfile\u Path++“worddoc”+“-”+“.docx”)代码>。