SaveFileDialog Help (C#)

I am new to the SaveFileDialog and I am not really understanding how to make it work. I cant seem to get it to save a file. Here is the code I have so far:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists("export.txt"))
                {
                    File.Delete("export.txt");
                }

                string siteSettings = "";
                string cameraSettings = "";
                string cogSettings = "";
                StreamReader readSiteSettings = new StreamReader("sites.txt");
                siteSettings = readSiteSettings.ReadToEnd();
                readSiteSettings.Close();

                StreamReader readCameraSettings = new StreamReader("cameras.txt");
                cameraSettings = readCameraSettings.ReadToEnd();
                readCameraSettings.Close();

                StreamReader readCogSettings = new StreamReader("customConfigurations.txt");
                cogSettings = readCogSettings.ReadToEnd();
                readCogSettings.Close();

                string exportFile = siteSettings + "!@#$" + cameraSettings + "!@#$" + cogSettings;

                StreamWriter export = new StreamWriter("export.txt");
                export.Write(exportFile);
                export.Close();


The file I want it to save is the export.txt file... but I really dont care what the user calls it. But every time I try to save the file it doesnt save. What am I doing wrong? Thanks.
Pretzel_JesusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

silemoneCommented:
place some breakpoints through out the code (start at the if statement and see if that translates to true...

use F9 key to create a breakpoint by going to that line in visual studios...then Start with debugging and the page will automatically stop when it comes to the line with the break point (breakpoint = this is where i want the program to execute line by line so i can see values)....place mouse over variables and you will see all values that were set before reaching that line of code.  next to execute each line one by one, press F11...if it skips that block, that means your if statement is evaluating to false and your DialogResult.Ok is not set right...
Pretzel_JesusAuthor Commented:
The code runs through fine. I just dont know why its not saving. I guess my initial question is am I using the SaveFileDialog correctly? Because I can give it a name but I dont see where I associate the data that actually gets saved into the file with the desired file path / file name.
silemoneCommented:
yes you're using it correctly...did you debug as I stated?  because sometimes you can catch where the code is going astray, like a null value being returned, etc...or a file is considered open, etc...
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

zstapicCommented:
I think you've got a few things wrong here. You don't actually need to give a name to the file, if you are using SaveFileDialog, that's the point. When you run your program, and press say.. the save button, it will show you the dialog, and ask you how do you want to name your file. Try the code bellow, if it works, just replace hello world with your text or a string....

SaveFileDialog sfd = new SaveFileDialog();
 
                sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                sfd.FilterIndex = 1;
                //sfd.RestoreDirectory = true;
                sfd.Title = "Save as...";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                     TextWriter tw = new StreamWriter(sfd.FileName);
                     tw.WriteLine("Hello World");
                     tw.Close();
                }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
silemoneCommented:
well there's a constructor that allows file name, but i agree zstapic...
Pretzel_JesusAuthor Commented:
Zstapic:

Ok that makes sense... I guess the last part of it would be the file path. Does the "Filename" property contain the entire file path or only the actualy filename (<filename>.txt)?
Pretzel_JesusAuthor Commented:
Actually I just tried it and it does include the file path so this is exactly what I was looking for. Thanks very much!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.