Using saveFileDialog to write out a document as a new name/location

I have this:

strFileName which is an MS Word document's full path and name such as "c:\documents\myword.doc".  So:
strFileName="c:\documents\myword.doc";

What I want to do is run the saveFileDialog and let the user create a new file name and save it out to their chosen location.

Here's what I'm trying, but it DOES NOT WORK:

                  Stream myStream;
                  SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                  saveFileDialog1.Filter = "doc files (*.doc)|*.doc";
                  saveFileDialog1.FilterIndex = 2 ;
                  saveFileDialog1.RestoreDirectory = true ;
 
                  if(saveFileDialog1.ShowDialog() == DialogResult.OK)
                  {
                        if((myStream = saveFileDialog1.OpenFile()) != null)
                        {
                              // (fromFile) file to copy from:  strFileName
                              // (toFile) file to copy to:  saveFileDialog1.FileName
                              FileInfo fromFile = new FileInfo(strFileName);
                              StreamReader myStreamReader = new StreamReader(fromFile.OpenRead());
                              FileInfo toFile = fromFile.CopyTo(saveFileDialog1.FileName, true);
                              myStream.Close();
                        }
                  }

What am I doing wrong?  I'm creating a blank, zero-byte, word document.  I can't seem to get the contents of the old word document to be saved out as the new one.

LVL 20
Daniel Van Der WerkenIndependent ConsultantAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
I'm a VB.Net coder so hopefully my syntax isn't too way off.  I don't understand why you are messing with all the file stream business...just make a copy of the file:

               SaveFileDialog saveFileDialog1 = new SaveFileDialog();
               saveFileDialog1.Filter = "doc files (*.doc)|*.doc";
               saveFileDialog1.FilterIndex = 2 ;
               saveFileDialog1.RestoreDirectory = true ;
 
               if(saveFileDialog1.ShowDialog() == DialogResult.OK)
               {
                    System.IO.File.Copy(strFileName, saveFileDialog1.FileName, True);
               }
0
 
jpedefConnect With a Mentor Commented:
Why don't you just use CopyFile (or CopyFileEx if you want progress) winapi call?

BOOL CopyFile(
  LPCTSTR lpExistingFileName, // name of an existing file
  LPCTSTR lpNewFileName,      // name of new file
  BOOL bFailIfExists          // operation if file exists
);
0
 
Daniel Van Der WerkenIndependent ConsultantAuthor Commented:
I realized after my post, I don't need this line:

StreamReader myStreamReader = new StreamReader(fromFile.OpenRead());

This is C#, btw.  I forgot to mention that.  Doh!

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So is your problem fixed?

If not, did my suggestion work?
0
 
Daniel Van Der WerkenIndependent ConsultantAuthor Commented:
The problem *may* be that I'm trying to copy a Word document file that's already been opened by WebBrowser2.ExecWB().  I'm getting an indication the file is "in use" via a catch.  I'm testing this now.
0
 
Daniel Van Der WerkenIndependent ConsultantAuthor Commented:
Yeah.  Your suggestion was perfect.  Thanks for leading me in the right direction.  I gave jpdef 100 points because I didn't specify C#.  I hope you don't mind.
---Dan---
0
All Courses

From novice to tech pro — start learning today.