0

In my program I am currently using SaveFileDialog to output a .txt file to the user specified path like so:

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "filename.txt";

Nullable<bool> result = dlg.ShowDialog();

if (result == true)
{
      //Open file, print text, close file
      File.WriteAllLines(dlg.FileName, stringArray);
}

Now I am at a point where I need to add more files to this location. So instead of naming a single file and saving it, I need to name multiple files and save them to the location specified by SaveFileDialog.

I want my new structure to be something like this:

UPDATE:

//Call functions to create string arrays to write to .txt files
createArrayForFile1();
createArrayForFile2();

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "File1";

Nullable<bool> result = dlg.ShowDialog();

if (result == true)
{
      //Create, print, and close both files
      File.WriteAllLines(dlg.FileName, stringArray1);
      directory = Path.GetDirectoryName(dlg.FileName);
      Path.Combine(directory, "File2"); 
}

How do I write several files to a location specified by SaveFileDialog?

Eric after dark
  • 1,768
  • 4
  • 31
  • 79

1 Answers1

0

string directory = Path.GetDirectoryName(dlg.Filename); will get you the directory name. Then you can do Path.Combine(directory,"filename.txt"); or whatever for each file path you want to write.

EDIT: Use FolderBrowserDialog. Then after you show the dialog to the user, you can do dlg.SelectedPath.

user2320861
  • 1,391
  • 2
  • 11
  • 28
  • I'm sorry if I was unclear, but I am not writing file paths. The path is specified by what folder the user clicks on in the `SaveFileDialog`. When they press "Save" I want both of my files to be placed into that location. – Eric after dark Feb 27 '14 at 20:28
  • +1 for the correct approach. I would additionally recommend that you use the select folder method described by @McGarnagle instead of a save dialog in this instance (because you are saving multiple files). – BradleyDotNET Feb 27 '14 at 20:34
  • 1
    Use `FolderBrowserDialog`. Then after you show the dialog to the user, you can do `dlg.SelectedPath`. – user2320861 Feb 27 '14 at 20:35
  • The `FolderBrowserDialog` does not feel like the conventional way to save files. In the program that I am looking at as an example, `SaveFileDialog` is used to do this, are you sure there isn't a way to do this with `SaveFileDialog`? – Eric after dark Feb 27 '14 at 20:49
  • 1
    SaveFileDialog won't give you a directory to which you can save multiple files, it'll give you the filepath of the ONE file. That's why if you use SaveFileDialog to save ONE file you'll need to use `Path.GetDirectoryName(dlg.Filename)` to get the directory in which that one file was saved, so that you can then save more files to that same directory later. However, it's more direct to just get the directory itself using `FolderBrowserDialog` then you can write multiple files by appending the file names to the end of the directory name. – user2320861 Feb 27 '14 at 21:26
  • I am attempting to use `Path.GetDirectoryName(dlg.FileName)` to get the directory that I used to save the first file so that I can save more files to that path, like you mentioned. I have posted my current attempt in my question under UPDATE. It compiles, but it does not save both of my files to that location. Please take a look and advise me further if you can. – Eric after dark Mar 03 '14 at 19:25
  • 1
    I don't see where you write the second file. Add this as the last line in your if block: `string fileName2 = Path.Combine(directory, "File2"); File.WriteAllLines(fileName2, stringArray2);` – user2320861 Mar 03 '14 at 19:36