如何从当前文件夹中获取word文件的路径,而无需在c#中的其他机器上运行时更改路径

时间:2014-02-14 21:02:17

标签: c# highlight

这是我的代码。

如何在不更改路径的情况下在任何计算机上运行EXE?

    object fileName=  @"Sam.docx"; //The filepath goes her
           //The text to find goes here

    Word.Application word = new Word.Application();
    Word.Document doc = new Word.Document();
    string line;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
       // int counter = 0;


        System.IO.FileStream stream = System.Reflection.Assembly.GetExecutingAssembly().GetFile(@"Sample.txt");
        System.IO.StreamReader file =
        new System.IO.StreamReader(@"Sample.txt");
        //object matchWholeWord = true;

        while ((line = file.ReadLine()) != null)
        {

            // Define an object to pass to the API for missing parameters
            object missing = System.Type.Missing;
            doc = word.Documents.Open(ref   fileName,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing);
            string ReadValue = string.Empty;
            // Activate the document
            doc.Activate();

1 个答案:

答案 0 :(得分:0)

关于此帖子的标题:“如何从当前文件夹中获取word文件的路径”


使用以下属性检索应用程序(* .exe)执行目录:AppDomain.CurrentDomain.BaseDirectory

被告知

Environment.CurrentDirectory属性可能会产生误导,因为并不总是像您期望的那样返回当前目录。例如,如果您的应用程序作为Windows服务运行,Environment.CurrentDirectory将不会返回您的应用程序的* .exe目录。

<强>参考