读取流文件并写入指定的文件:
private void toolStripButtonSave_Click(object sender, EventArgs e) { if (System.IO.File.Exists(Common.UserPublic.TempFile)) { SaveFileDialog sfd = new SaveFileDialog(); sfd.RestoreDirectory = true; sfd.Filter = "*.disclosure|*.disclosure"; sfd.InitialDirectory = System.Windows.Forms.Application.StartupPath + "//Document//"; sfd.FileName = Common.UserPublic.NodeValue + ".disclosure"; if (sfd.ShowDialog() == DialogResult.OK) { this.axFramerControlMain.Close(); if (GetDisclosure(sfd.FileName)) { MessageBox.Show("生成成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } private bool GetDisclosure(string file=null) { try { using (System.IO.FileStream fsRead = new System.IO.FileStream(Common.UserPublic.TempFile, System.IO.FileMode.Open)) { int fsLen = (int)fsRead.Length; byte[] heByte = new byte[fsLen]; using (System.IO.FileStream fsWrite = new System.IO.FileStream(file, System.IO.FileMode.Append)) { fsWrite.Write(heByte, 0, heByte.Length); }; } return true; } catch { return false; } }
从刚才写入的文件中再读取回来:
private void toolStripButtonOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "*.disclosure|*.disclosure"; if (ofd.ShowDialog() == DialogResult.OK) { if (OpenDisclosure(ofd.FileName)) { this.BeginInvoke(new MethodInvoker(delegate() { OpenMaking(); })); } } } private bool OpenDisclosure(string file = null) { Common.UserPublic.TempFile = Application.StartupPath + "\\Temp\\" + "PC-1" + ".docx"; Common.UserPublic.RtfFile = Application.StartupPath + "\\Infroduce\\" + "PC-1" + ".rtf"; try { using (System.IO.FileStream fsRead = new System.IO.FileStream(file, System.IO.FileMode.Open)) { int fsLen = (int)fsRead.Length; //byte[] heByte = System.Text.Encoding.Unicode.GetBytes(fsRead.); byte[] heByte = new byte[fsLen]; using (System.IO.FileStream fsWrite = new System.IO.FileStream(Common.UserPublic.TempFile, System.IO.FileMode.Append)) { fsWrite.Write(heByte, 0, heByte.Length); }; } return true; } catch { return false; } } public void OpenMaking() { this.axFramerControlMain.Dispose(); this.richTextBoxIntroduce.LoadFile(Common.UserPublic.RtfFile); Thread.Sleep(200); this.axFramerControlMain.Open(Common.UserPublic.TempFile); Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)this.axFramerControlMain.ActiveDocument; doc.Application.WindowSelectionChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(this.SelectEvent); }
到OpenMaking()里面的
this.axFramerControlMain.Open(Common.UserPublic.TempFile);就报错了
我用word打开文件的时候也不行,也就是说在之前转换的时候出错了,我应该怎么设置word文件转换成流文件的格式呢?
楼主怎么解决的呢?
你猜