C#直接保存到桌面文件夹,没有保存文件对话框 [英] C# save directly to a desktop folder w/o save file dialogue

查看:199
本文介绍了C#直接保存到桌面文件夹,没有保存文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经使用SaveFileDialogue保存了这段代码,但我的目的是静默保存到桌面文件夹:



So far I have this code that saves using a SaveFileDialogue, but what im looking is to save silently to a desktop folder:

//Word to PDF Conversion
        private void button4_Click_1(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
            wordDocument = appWord.Documents.Open(textBox4.Text);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PDF Documents|*.pdf";
            sfd.FileName = textBox4.Text;
            try
            {
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string ext = System.IO.Path.GetExtension(sfd.FileName);
                    switch (ext)
                    {
                        case ".pdf":
                            wordDocument.ExportAsFixedFormat(sfd.FileName, WdExportFormat.wdExportFormatPDF);
                            break;
                        case ".docx":
                            wordDocument.ExportAsFixedFormat(sfd.FileName, WdExportFormat.wdExportFormatPDF);
                            break;

                    }
                    textBox4.Text = sfd.FileName;
                }
                System.Windows.Forms.MessageBox.Show("Files Converted Successfully..");
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            System.Diagnostics.Process.Start(sfd.FileName);
            Dispose();
        }

        public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }





我尝试过:



尝试添加sfd.FileName = textBox4.Text;在保存文件对话框上但这只是将文件路径添加到保存文件对话框



What I have tried:

Tried adding sfd.FileName = textBox4.Text; on the save file dialogue but this only adds the file path to the save file dialogue

推荐答案

所以只需删除所有与SaveFileDialog有关的代码。您使用SFD的唯一方法是从用户获取文件路径。而已。没有别的。



你所要做的就是为ExportAsFixedFormat提供一个文件名和格式来获取Word来保存文件。



当然,您必须获取用户Desktop文件夹的路径,如果您使用 Environment.GetFolderPath() [ ^ ]。您传入的SpecialFolder是DesktopDirectory。这将为您提供用户Desktop文件夹的路径。使用该路径构建要保存的文件的完整路径,并将文件名传递给Path.Combine()。
So just remove all the code has has anything to do with the SaveFileDialog. The only thing you use a SFD for is to get a filepath from the user. That's it. Nothing else.

All you have to do is supply a filename and format to ExportAsFixedFormat to get Word to save the file.

You, of course, have to get the path to the users Desktop folder, which is very easy if you use Environment.GetFolderPath()[^]. The SpecialFolder you pass in is DesktopDirectory. This will give you the path to the users Desktop folder. Build your complete path to the file you want to save using that path and your filename passed to Path.Combine().


试试这个



try this

Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
          wordDocument = appWord.Documents.Open(textBox4.Text);
           string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
          string file = textBox4.Text;
          string path = Path.Combine( desktop , file);

          try
          {
                  string ext = System.IO.Path.GetExtension(path);
                  switch (ext)
                  {
                      case ".pdf":
                          wordDocument.ExportAsFixedFormat(path, WdExportFormat.wdExportFormatPDF);
                          break;
                      case ".docx":
                          wordDocument.ExportAsFixedFormat(path, WdExportFormat.wdExportFormatPDF);
                          break;

                  }

              }





警告:您已声明 public Microsoft.Office.Interop.Word.Document wordDocument {get;组; } 这个外部,总是正确地处理这个对象,否则 Word 的新实例将位于你的TaskManager中。



Warning: you have declared public Microsoft.Office.Interop.Word.Document wordDocument { get; set; } this outside, Always dispose this object properly, else a new instance of Word will be sitting in your TaskManager .


这篇关于C#直接保存到桌面文件夹,没有保存文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆