文件流问题文件未打开

本文关键字:文件 问题 | 更新日期: 2023-09-27 18:04:08

这里我将参数写入已创建的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。打开方法:

在指定路径上打开FileStream

实际上您正在尝试打开带有相关应用程序的文件:

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

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