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"))

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

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

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

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

                StreamWriter export = new StreamWriter("export.txt");

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.
Who is Participating?
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");

Open in new window

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) 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.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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...
well there's a constructor that allows file name, but i agree zstapic...
Pretzel_JesusAuthor Commented:

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.