?
Solved

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

Posted on 2005-03-17
6
Medium Priority
?
624 Views
Last Modified: 2012-05-05
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.

0
Comment
Question by:Daniel Van Der Werken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1600 total points
ID: 13572073
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
 
LVL 3

Assisted Solution

by:jpedef
jpedef earned 400 total points
ID: 13572612
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
 
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 13574830
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13574896
So is your problem fixed?

If not, did my suggestion work?
0
 
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 13575113
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
 
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 13575788
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question