SaveFileDialog Help (C#)

Posted on 2008-11-12
Last Modified: 2013-12-17
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.
Question by:Pretzel_Jesus
    LVL 21

    Expert Comment

    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...

    Author Comment

    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.
    LVL 21

    Expert Comment

    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...
    LVL 4

    Accepted Solution

    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

    LVL 21

    Expert Comment

    well there's a constructor that allows file name, but i agree zstapic...

    Author Comment


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

    Author Closing Comment

    Actually I just tried it and it does include the file path so this is exactly what I was looking for. Thanks very much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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.

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    794 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

    18 Experts available now in Live!

    Get 1:1 Help Now