• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 904
  • Last Modified:

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.
0
Pretzel_Jesus
Asked:
Pretzel_Jesus
  • 3
  • 3
1 Solution
 
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...
0
 
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.
0
 
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...
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

0
 
silemoneCommented:
well there's a constructor that allows file name, but i agree zstapic...
0
 
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)?
0
 
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!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now