Link to home
Start Free TrialLog in
Avatar of Pretzel_Jesus
Pretzel_Jesus

asked on

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.
Avatar of silemone
silemone
Flag of United States of America image

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...
Avatar of Pretzel_Jesus
Pretzel_Jesus

ASKER

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.
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...
ASKER CERTIFIED SOLUTION
Avatar of zstapic
zstapic
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well there's a constructor that allows file name, but i agree zstapic...
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)?
Actually I just tried it and it does include the file path so this is exactly what I was looking for. Thanks very much!