文件流问题文件打不开

File Stream issue file not opening

这里我正在将参数写入一个已经创建的word文件,并在创建相关文件后将其保存到一个新文件应该打开:

public void CreateWordDocument(object fileName,
                                    object saveAs, string nbt, string vat,string total)
    {
        Word._Application wApp = new Word.Application();
        //Set Missing Value parameter - used to represent
        // a missing value when calling methods through
        // interop.
        object missing = System.Reflection.Missing.Value;

        //Setup the Word.Application class.
        Word.Application wordApp = 
            new Word.ApplicationClass();

        //Setup our Word.Document class we'll use.
        Word.Document aDoc = null;

        // Check to see that file exists
        if (File.Exists((string)fileName))
        {
            DateTime today = DateTime.Now;

            object readOnly = false;
            object isVisible = false;
            //Set Word to be not visible.
            wordApp.Visible = false;
            //Open the word document
            aDoc = wordApp.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);
            // Activate the document
            aDoc.Activate();                
            // Find Place Holders and Replace them with Values.
            this.FindAndReplace(wordApp, "NBT",nbt);
            this.FindAndReplace(wordApp, "VAT", vat);
            this.FindAndReplace(wordApp, "TOTAL", total)                                                
        }
        else
        {
            MessageBox.Show("File dose not exist.");
            return;
        }
        //Save the document as the correct file name.
        aDoc.SaveAs(ref saveAs, 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);
        //Close the document - you have to do this.
        aDoc.Close(ref missing, ref missing, ref missing);
        MessageBox.Show("File created.");
        wordApp.Visible = true;            
        //using (var filestresam = new FileStream(@"D:\Invoice\new.docx", FileMode.Open, FileAccess.Read));
        using (FileStream stream =File.Open(@"D:\Invoice\new.docx",FileMode.Open,FileAccess.Read,FileShare.Read));

    }
    private void FindAndReplace(Word.Application WordApp, 
                                object findText, 
                                object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        WordApp.Selection.Find.Execute(ref findText,
            ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike,
            ref nmatchAllWordForms, ref forward,
            ref wrap, ref format, ref replaceWithText,
            ref replace, ref matchKashida,
            ref matchDiacritics, ref matchAlefHamza, 
            ref matchControl);
    }

之外,其他一切正常
using (FileStream stream =File.Open(@"D:\Invoice\new.docx",FileMode.Open,FileAccess.Read,FileShare.Read));

相关文件打不开,没有显示错误也找不到问题所在

那个 using 语句行以冒号结尾:它没有正文。

因此文件将被打开,并立即关闭。

你需要在他们里面放一些东西来对打开的文件做一些事情:

using (var strm = File.Open(…)) {
  strm.Write(some data);
}

您似乎期望 File.Open() 实际上使用与文件扩展名关联的程序打开文件。

这不是该方法的作用,请参阅 MSDN: File.Open Method:

Opens a FileStream on the specified path.

事实上你正试图 Open file with associated application:

System.Diagnostics.Process.Start(@"D:\Invoice\new.docx");

请注意,这在 ASP.NET 上下文中不起作用,但仅在客户端实际 运行 的应用程序中起作用。