Solved

SaveFileDialog issue

Posted on 2010-09-16
32
769 Views
Last Modified: 2013-12-16
Experts
I have a question that is how to copy the content of a file while using SaveFileDialog in c#.
That is in the following code what actually I have to do so that the selected file get copied to the destination location.


private void button6_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    StreamWriter wText = new StreamWriter(myStream);

                    wText.Write(" your text");

                    myStream.Close();
                }
            }
        }

thanking you,
private void button6_Click(object sender, EventArgs e)

        {

            Stream myStream;

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();



            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

            saveFileDialog1.FilterIndex = 2;

            saveFileDialog1.RestoreDirectory = true;



            if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {

                if ((myStream = saveFileDialog1.OpenFile()) != null)

                {

                    StreamWriter wText = new StreamWriter(myStream);



                    wText.Write(" your text");



                    myStream.Close();

                }

            } 

        }

Open in new window

0
Comment
Question by:ANINDYA
  • 13
  • 11
  • 8
32 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33689713
Not sure what you are trying to do here.

You want to copy the selected file to a pre defined location ?
0
 

Author Comment

by:ANINDYA
ID: 33689725
Expert gauthampj
you are right .
The basic operation of saveas operation is the one which I want to implement.
That is all
Thanking you
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33689728
if so then try this
private void button6_Click(object sender, EventArgs e)

        {

            Stream myStream;

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();



            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

            saveFileDialog1.FilterIndex = 2;

            saveFileDialog1.RestoreDirectory = true;



            if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {

                if ((myStream = saveFileDialog1.OpenFile()) != null)

                {

                    byte [] temp = new byte[myStream.Length];

                    myStream.Read(temp, 0, temp.Length);

                    System.IO.File.WriteAllBytes("path you want to copy to",temp);

                    myStream.Close();

                }

            }

        }

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33689735
or
private void button6_Click(object sender, EventArgs e)

        {

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();



            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

            saveFileDialog1.FilterIndex = 2;

            saveFileDialog1.RestoreDirectory = true;



            if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {

                System.IO.File.Copy(saveFileDialog1.FileName, "path to copy to");

            }

        }

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33689809

SaveFileDialog saveFileDialog1 = new SaveFileDialog();



            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

            saveFileDialog1.FilterIndex = 2;

            saveFileDialog1.RestoreDirectory = true;



            if (saveFileDialog1.ShowDialog() == DialogResult.OK)

            {

                string filename = saveFileDialog1.FileName;



                if (File.Exists(filename))

                {

                    File.Copy(filename, "Destination Path", false);  // Change the last param to true if you wish to overwrite.

                }

            }

Open in new window

0
 

Author Comment

by:ANINDYA
ID: 33689811
Expert
here actually I am trying to copy the file a.txt from desktop to  D: drive with the same name .
and I am facing this following error.
Sir I have a question that is the second parameter of the Copy function takes the string Name of the destination file .

So sir how could it come in your code .
As you are passing a fixed string there. and moreover that is not the file name . Would you please explain that.
Thanking you
error.JPG
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33689831
Can you tell us the exact flow of your application

say
1. SaveDialog Captures the file destination
2. Where's the source file comes from?

and any other details you wish to add
0
 

Author Comment

by:ANINDYA
ID: 33689860
Sir
Objectives are
1.There is a file in the drive( it may be any drive)
2.Here I am using a winform application .
3.I am supposed to copy that file to my desired specific location .( it is not fixed locaiton .User may wish to change it as he wishes)
4.As the file will get saved to a user specific location there after that location should get saved to my table .That is the path of the file should get saved to my Table.

This is the objective
Thanking you

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33689882

SaveFileDialog srcDialog = new SaveFileDialog();



            srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

            srcDialog.FilterIndex = 2;

            srcDialog.RestoreDirectory = true;



            if (srcDialog.ShowDialog() == DialogResult.OK)

            {

                FolderBrowserDialog destDir = new FolderBrowserDialog();



                if (destDir.ShowDialog() == DialogResult.OK)

                {

                    string filename = saveFileDialog1.FileName;

                    string destinationFolder = destDir.SelectedPath;



                    if (File.Exists(filename) && Directory.Exists(destinationFolder))

                    {

                        File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.

                    }

                }

            }

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33689895
A more refined version
            using (SaveFileDialog srcDialog = new SaveFileDialog())

            {

                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                srcDialog.FilterIndex = 2;

                srcDialog.RestoreDirectory = true;



                if (srcDialog.ShowDialog() == DialogResult.OK)

                {

                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())

                    {

                        if (destDir.ShowDialog() == DialogResult.OK)

                        {

                            string filename = srcDialog.FileName;

                            string destinationFolder = destDir.SelectedPath;



                            if (File.Exists(filename) && Directory.Exists(destinationFolder))

                            {

                                File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.

                            }

                        }

                    }

                }

            }

Open in new window

0
 

Author Comment

by:ANINDYA
ID: 33690040
Expert Novice Novice
Please see the attached image.
Thanking you

error.JPG
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33690054
it would be better to post the error instead of posting images like this.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33690073
also..i don't think you put the code correctly it still shows "path to copy to" which should be replaced by the actual path
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33690084
replace the method private void button6_Click with the code given by novoice
private void button6_Click(object sender, EventArgs e)

        {

                        using (SaveFileDialog srcDialog = new SaveFileDialog())

            {

                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                srcDialog.FilterIndex = 2;

                srcDialog.RestoreDirectory = true;



                if (srcDialog.ShowDialog() == DialogResult.OK)

                {

                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())

                    {

                        if (destDir.ShowDialog() == DialogResult.OK)

                        {

                            string filename = srcDialog.FileName;

                            string destinationFolder = destDir.SelectedPath;



                            if (File.Exists(filename) && Directory.Exists(destinationFolder))

                            {

                                File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.

                            }

                        }

                    }

                }

            }

        }

Open in new window

0
 

Author Comment

by:ANINDYA
ID: 33690093
Expert gauthampj
The path has to be selected by the user .
Now you on ly tell me how can I determine the path while coding ?
it is to be flexible.
Thanking you
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33690105
if you look at line number 11 , it is allowing the user to select the folder to which the file has to be copied to. Please replace you entire  code with the with the one that i have posted
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ANINDYA
ID: 33690122
Expert gauthampj
 Novice's code is not generating any error .
But the case is it is not coping the file to any destination .

the code which I am using is your code only expert gauthampj
Thanking you
using (SaveFileDialog srcDialog = new SaveFileDialog())

            {

                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                srcDialog.FilterIndex = 2;

                srcDialog.RestoreDirectory = true;



                if (srcDialog.ShowDialog() == DialogResult.OK)

                {

                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())

                    {

                        if (destDir.ShowDialog() == DialogResult.OK)

                        {

                            string filename = srcDialog.FileName;

                            string destinationFolder = destDir.SelectedPath;



                            if (File.Exists(filename) && Directory.Exists(destinationFolder))

                            {

                                File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.

                            }

                        }

                    }

                }

            }

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33690146
update the code as such and tell me what happens
using (SaveFileDialog srcDialog = new SaveFileDialog())

            {

                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                srcDialog.FilterIndex = 2;

                srcDialog.RestoreDirectory = true;



                if (srcDialog.ShowDialog() == DialogResult.OK)

                {

                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())

                    {

                        if (destDir.ShowDialog() == DialogResult.OK)

                        {

                            string filename = srcDialog.FileName;

                            string destinationFolder = destDir.SelectedPath;

if(!File.Exists(filename))

{

MessageBox("Source file not present");

}



if(!Directory.Exists(destinationFolder))

{

MessageBox("Destination folder not present");

}

                            if (File.Exists(filename) && Directory.Exists(destinationFolder))

                            {

                                File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.

                            }

                        }

                    }

                }

            }

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33690175
I'm looking at your last image and I see you've mixed things up. It will be good to post the complete good to help us refine it correctly.

The image shows three dialogs wherein i've made changes to show only two - SourceFile selector and Destination folder selector.

If you wish to allow the User to rename then replace FolderBrowser with SaveFileDialog.

ANother version of code which should exactly the way you need it.
        private void button6_Click(object sender, EventArgs e)

        {

            using (OpenFileDialog srcDialog = new OpenFileDialog())

            {

                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                srcDialog.FilterIndex = 2;

                srcDialog.RestoreDirectory = true;



                if (srcDialog.ShowDialog() == DialogResult.OK)

                {

                    using (SaveFileDialog destDir = new SaveFileDialog())

                    {

                        destDir.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                        destDir.FilterIndex = 2;

                        destDir.RestoreDirectory = true;



                        if (destDir.ShowDialog() == DialogResult.OK)

                        {

                            string filename = srcDialog.FileName;

                            string destinationFolder = destDir.FileName;



                            if (File.Exists(filename) && Directory.Exists(destinationFolder))

                            {

                                File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.

                            }

                        }

                    }

                }

            }

        }

Open in new window

0
 

Author Comment

by:ANINDYA
ID: 33690234
Expert gauthampj
please see that it is not coping
thanking you
error.JPG
0
 

Author Comment

by:ANINDYA
ID: 33690260
Expert Novice Novice
your latest code



 private void button6_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog srcDialog = new OpenFileDialog())
            {
                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                srcDialog.FilterIndex = 2;
                srcDialog.RestoreDirectory = true;

                if (srcDialog.ShowDialog() == DialogResult.OK)
                {
                    using (SaveFileDialog destDir = new SaveFileDialog())
                    {
                        destDir.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                        destDir.FilterIndex = 2;
                        destDir.RestoreDirectory = true;

                        if (destDir.ShowDialog() == DialogResult.OK)
                        {
                            string filename = srcDialog.FileName;
                            string destinationFolder = destDir.FileName;

                            if (File.Exists(filename) && Directory.Exists(destinationFolder))
                            {
                                File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.
                            }
                        }
                    }
                }
            }
        }




is not coping the file to the destination drive.
Thanking you
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33690283
you are selecting an invalid source file so  the check File.Exists(filename) fails and hence it fails to copy
0
 

Author Comment

by:ANINDYA
ID: 33690323
Expert gauthampj
Please sir you see it  .I have checked it sir .It is not coping any file .
now only I have tried 5 files of .jpeg,.txt etc.

Sir for your satisfaction also I am saying it is my humble request please see in your system also .
It is not coping any file .
Thanking you
0
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 200 total points
ID: 33690340
try this....remove your method and copy this method...
private void button6_Click(object sender, EventArgs e)

        {

            using (OpenFileDialog srcDialog = new OpenFileDialog())

            {

                srcDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                srcDialog.FilterIndex = 2;

                srcDialog.RestoreDirectory = true;



                if (srcDialog.ShowDialog() == DialogResult.OK)

                {

                    using (FolderBrowserDialog destDir = new FolderBrowserDialog())

                    {

                        if (destDir.ShowDialog() == DialogResult.OK)

                        {

                            string filename = srcDialog.FileName;

                            string destinationFolder = destDir.SelectedPath;



                            if (File.Exists(filename) && Directory.Exists(destinationFolder))

                            {

                                destinationFolder += "\\"+Path.GetFileName(filename);

                                File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.

                            }

                        }

                    }

                }

            }

        }

Open in new window

0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 300 total points
ID: 33690372
The error in my code is this statement

remove it

 if (File.Exists(filename) && Directory.Exists(destinationFolder))

so it should look like

f (destDir.ShowDialog() == DialogResult.OK)
                        {
                            string filename = srcDialog.FileName;
                            string destinationFolder = destDir.FileName;

                            File.Copy(filename, destinationFolder, false);  // Change the last param to true if you wish to overwrite.
                         
                        }
0
 

Author Closing Comment

by:ANINDYA
ID: 33690453
Expert gauthampj and Novice Novice
Your respective latest  codes are working exactly as per my requirement .
I have seen both of your latest codes are working without error.
It is giving exact result.
Thanks a lot for the answer and the codes which you have rendered to me .
Thanks for the patience and providing the full code and spending your valuable time for my help.
hope to get your help in future too.
Thanking you very very much
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33690460
ANINDYA,

Quite a strange that what gauthampj has corrected is only my code and he get's the full points. Shouldn't that be other way - I get full points and he gets assisted?
0
 

Author Comment

by:ANINDYA
ID: 33690514
Expert Novice Novice
I have accepted the button "Accept multiple Solution ".
There after I have given both of you experts the points as Accepted .
I do not know frankly speaking what went wrong from my side .
Or it may be I do not how to select someone as assisted and accepted.
Anyway I have a deepest respect for both of you .
May be I have by mistake ( you got assisted ) .
Please expert Novice Novice do not take it from heart.
I am a fresher from India.
I do post several questions every day .
Please track on my name .
My name is Anindya
Next time I will not make any mistake.
I am posting a question now only so keep track.
For making so mistake and If I have heart you for that I am extremely sorry.
Thanks a lot Novice Novice.
Thanks for rendering help to me.

Anindya Chatterjee
Bangalore
India
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33690532
You can still correct things from your end.

Click Request Attention on the first post (the question) and you should be able to do that with the help of an Admin.

And there's not need say where you come from and what you do. You should always maintain neutrality while on public domain.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33690584
Novice_Novice >>> brighten up and cool down.

ANINDYA >>> you can give full point to Novice_Novice , my primary aim in these forums is to help and to learn by doing to do so.
0
 

Author Comment

by:ANINDYA
ID: 33690619
Expert Novice Novice
I have requested to Admin for the change.


Please check the following link

http://www.experts-exchange.com/Community_Support/General/Q_26477447.html

Thanking you
Anindya Chatterjee
Bangalore
0
 

Author Comment

by:ANINDYA
ID: 33690636
Expert gauthampj
How can I deprive you as your code is also working .
Moreover you have also rendered help to me.
I have fullest gratitude towatds you and Novice Novice
Thanking you
Anindya Chatterjee
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Application Local DB Connection String 23 62
Image(7) 1 37
Make a border less form movable 2 14
Events in static methods 3 31
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now