打开应用程序文件夹中的文件

用户名

我比较新,但是我希望能够将我的应用程序转移到其他计算机上。显然,如果将文件传输到另一台计算机,文件路径将有所不同。在此应用程序中,它将打开一个要键入的模板。我尝试将路径简化为多组内容,但是它尝试从Windows \ system32 \而不是它所在的文件夹中打开文件。关于此用途我有什么想法?一个简单的搜索为我提供了诸如GetDirectory之类的各种功能,但我不确定要在我已编写的代码中将其放置在何处,在任何地方,我都会遇到一些错误。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace WindowsFormsApplication2
{


public partial class Form1 : Form
{
    Word.Application objWord = new Word.Application();
    Word.Document objDoc;
    public Form1()
    {
        InitializeComponent();
        objWord.Visible = true;
        object isVisible = true;
        object readOnly = true;

        object Missing = System.Reflection.Missing.Value;
        object fileName = @"release/MLA1.dotx";
        objDoc = objWord.Documents.Open(ref fileName, ref Missing, ref readOnly, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref isVisible, ref Missing, ref Missing, ref Missing, ref Missing);

    }


    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }


    private void SearchReplace()
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {



        Word.Find findObject = objWord.Selection.Find;
        findObject.ClearFormatting();
        findObject.Text = "Name";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = textBox1.Text;

        object replaceAll = Word.WdReplace.wdReplaceAll;
        object missing = System.Reflection.Missing.Value;
        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        findObject.ClearFormatting();
        findObject.Text = "Teacher";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = textBox2.Text;


        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        findObject.ClearFormatting();
        findObject.Text = "Class";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = textBox3.Text;


        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        findObject.ClearFormatting();
        findObject.Text = "Date";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = textBox4.Text;


        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        findObject.ClearFormatting();
        findObject.Text = "Title";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = textBox5.Text;


        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        foreach (Word.Section wordSection in objDoc.Sections)
        {
            foreach (Word.HeaderFooter wordHeader in wordSection.Headers)
            {
                Word.Range docRange = wordHeader.Range;

                docRange.Find.ClearFormatting();
                docRange.Find.Text = "Name";
                docRange.Find.Replacement.ClearFormatting();
                docRange.Find.Replacement.Text = textBox1.Text;


                docRange.Find.Execute(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                          ref replaceAll, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }
        }

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
}
杰里米·汤普森(Jeremy Thompson)

尝试使用Application.StartupPath

object fileName = Application.StartupPath + "release\\MLA1.dotx";

在调试模式下,您可能想要执行以下操作以确保其在根文件夹中查找(该exe将在运行时进入):

object fileName = Application.StartupPath.Replace("bin\Debug", String.Empty) + "release\\MLA1.dotx";

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

应用扩展程序如何访问包含应用程序文档/文件夹中的文件

来自分类Dev

如何在GNOME应用程序菜单中创建应用程序文件夹?

来自分类Dev

如何在GNOME应用程序菜单中创建应用程序文件夹?

来自分类Dev

ASP C#中来自fileUpload的应用程序文件夹中的SaveAs()文件

来自分类Dev

如何指向应用程序文件夹?

来自分类Dev

Composer Web应用程序文件夹结构

来自分类Dev

斯威夫特:如何确定应用程序文件夹?

来自分类Dev

邮编Web应用程序文件夹

来自分类Dev

Android Studio创建应用程序文件夹

来自分类Dev

启动应用程序文件夹位置

来自分类Dev

斯威夫特:如何确定应用程序文件夹?

来自分类Dev

Ubuntu 20.04 应用程序文件夹

来自分类Dev

Spring Server自动删除存储在应用程序文件夹中的文件

来自分类Dev

使用Flask应用程序文件夹中的png文件

来自分类Dev

cordova 应用程序文件夹中没有 .xcodeproj 文件

来自分类Dev

Applescript 重命名应用程序文件夹中的文件

来自分类Dev

升级android studio时,“应用程序文件夹中的Android SDK文件夹”

来自分类Dev

在codeigniter中,如何通过将资产文件夹放在应用程序文件夹中使其工作?

来自分类Dev

在 CodeIgniter 中,如何使用 site_url 访问应用程序文件夹之外的文件夹

来自分类Dev

如何设置图像驻留在应用程序文件夹中的Image Control的源

来自分类Dev

xcodebuild不会安装到应用程序文件夹中

来自分类Dev

在Ubuntu中查看包含应用程序文件夹的命令

来自分类Dev

PHP:如何在.htaccess中隐藏我的应用程序文件夹

来自分类Dev

在Ubuntu中查看包含应用程序文件夹的命令

来自分类Dev

我可以仅备份Windows中的应用程序文件夹吗?

来自分类Dev

新CodeIgniter安装中的现有CodeIgniter应用程序文件夹

来自分类Dev

无法在Codeigniter中访问应用程序文件夹的内容

来自分类Dev

如何永久禁用GNOME菜单中的应用程序文件夹?

来自分类Dev

如何设置图像驻留在应用程序文件夹中的Image Control的源

Related 相关文章

  1. 1

    应用扩展程序如何访问包含应用程序文档/文件夹中的文件

  2. 2

    如何在GNOME应用程序菜单中创建应用程序文件夹?

  3. 3

    如何在GNOME应用程序菜单中创建应用程序文件夹?

  4. 4

    ASP C#中来自fileUpload的应用程序文件夹中的SaveAs()文件

  5. 5

    如何指向应用程序文件夹?

  6. 6

    Composer Web应用程序文件夹结构

  7. 7

    斯威夫特:如何确定应用程序文件夹?

  8. 8

    邮编Web应用程序文件夹

  9. 9

    Android Studio创建应用程序文件夹

  10. 10

    启动应用程序文件夹位置

  11. 11

    斯威夫特:如何确定应用程序文件夹?

  12. 12

    Ubuntu 20.04 应用程序文件夹

  13. 13

    Spring Server自动删除存储在应用程序文件夹中的文件

  14. 14

    使用Flask应用程序文件夹中的png文件

  15. 15

    cordova 应用程序文件夹中没有 .xcodeproj 文件

  16. 16

    Applescript 重命名应用程序文件夹中的文件

  17. 17

    升级android studio时,“应用程序文件夹中的Android SDK文件夹”

  18. 18

    在codeigniter中,如何通过将资产文件夹放在应用程序文件夹中使其工作?

  19. 19

    在 CodeIgniter 中,如何使用 site_url 访问应用程序文件夹之外的文件夹

  20. 20

    如何设置图像驻留在应用程序文件夹中的Image Control的源

  21. 21

    xcodebuild不会安装到应用程序文件夹中

  22. 22

    在Ubuntu中查看包含应用程序文件夹的命令

  23. 23

    PHP:如何在.htaccess中隐藏我的应用程序文件夹

  24. 24

    在Ubuntu中查看包含应用程序文件夹的命令

  25. 25

    我可以仅备份Windows中的应用程序文件夹吗?

  26. 26

    新CodeIgniter安装中的现有CodeIgniter应用程序文件夹

  27. 27

    无法在Codeigniter中访问应用程序文件夹的内容

  28. 28

    如何永久禁用GNOME菜单中的应用程序文件夹?

  29. 29

    如何设置图像驻留在应用程序文件夹中的Image Control的源

热门标签

归档