在应用程序的文件夹中打开一个文件

时间:2016-07-21 01:06:57

标签: c# ms-word

我比较新,但我希望能够将我的应用程序转移到不同的计算机上。显然,如果我将文件传输到另一台计算机,文件路径会有所不同。在这个应用程序中,它打开了一个键入的模板。我尝试缩短了多个东西的路径,但是它尝试从windows \ system32 \而不是它所在的文件夹中打开文件。我应该使用什么想法?一个简单的搜索给了我各种各样的东西,比如GetDirectory,但是我不完全确定将它放在我已编写的代码中的位置,Anyplace我尝试了一些错误。

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();
    }
}
}

1 个答案:

答案 0 :(得分:1)

尝试使用Application.StartupPath

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

在调试模式下,您可能希望这样做以确保它在根文件夹中查找(exe文件将位于RunTime中):

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