-1

I’d like to write text to the file when the User specifies the save location, but this keeps returning an exception about unsupported file path. Where do I find the filepath?

private void SaveAs_Click(object sender, System.EventArgs e)
{
    Stream myStream;
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();

    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        if ((myStream = saveFileDialog1.OpenFile()) != null)
        {
            string createText = "Hello and Welcome" + Environment.NewLine;

            string path = saveFileDialog1.ToString();
            File.WriteAllText(path, createText);
            myStream.Close();
        }
    } 
}
KeLorean
  • 3
  • 5
  • Or you use File.WriteAllText or you use OpenFile from the SaveFileDialog. You need to choose one – Steve Aug 26 '19 at 20:29
  • You have to write to `myStream`. `saveFileDialog1.ToString();` doesn't mean anything. You'll just get the object name. Or use [FileDialog.FileName](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.filename) and write there the way you want. – Jimi Aug 26 '19 at 20:30
  • 3
    I would suggest learning how to debug your code. Set a breakpoint on `string path = saveFileDialog1.ToString();` and see what `path` actually resolves to. Or Console.Write it to console and see. What path ends up being might surprise you – DetectivePikachu Aug 26 '19 at 20:37
  • Can't overstate the importance of debugging. This time someone else can look at your code and spot the problem. Next time they won't be able to. Without it, writing code will not be difficult. It will be impossible. Also, unit tests. That has nothing to do with this question - just a PSA. This: [Navigate through code with the Visual Studio debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2019) – Scott Hannen Aug 26 '19 at 21:04
  • @ĴošħWilliard thanks for that. admittedly, i don't debug enough in the conventional way, bc VS shows me where the problem is most of the time. my post is probably not that clear either, bc i think im not so good at explaining things, but it was clear to me that lines: string path... and File.WriteAllText... were completely wrong. Nonetheless, I abandoned this after troubleshooting for amost an entire day, and just found another approach that worked just fine. thx anyway. – KeLorean Aug 27 '19 at 13:56
  • VS can show you compiler errors, but a lot of the time like this your issue will be logical errors that the compiler cant infer. Start setting break points at the beginning of troubling functions and just step through one line at a time and see what the variables are and see if they are what they should be. As you do it more you will get better and faster at it. It is truly a required skill. – DetectivePikachu Aug 28 '19 at 01:15

1 Answers1

2

To get the path selected in the SaveFileDialog, you don't call ToString() on it, instead, you use the FileName property.

Here's a quick example:

string path = saveFileDialog1.FileName;
File.WriteAllText(path, createText);

SaveFileDialog Docs

Larry Tang
  • 642
  • 5
  • 23
  • all I get is: System.IO.IOException: 'The process cannot access the file 'C:\...' because it is being used by another process.' – KeLorean Aug 26 '19 at 21:04
  • 1
    That's a different problem. The original problem was the the path you were trying to write to wasn't really a path. It was something like "System.Windows.Forms.SaveFileDialog". Now the problem is that the file is open. Check to see if you have it open in Notepad. – Scott Hannen Aug 26 '19 at 21:08
  • @ScottHannen I just created it. How can it be opened in another application? – KeLorean Aug 26 '19 at 21:14
  • Check if you have any programs open editing the file, and if not, restart your PC and try again. Also, if you used `File.Create`, it doesn't actually close the file, it stays locked. – Larry Tang Aug 26 '19 at 21:15
  • Already described: OR you use the stream returned by `SaveFileDialog.OpenFile()` OR you use `SaveFileDialog.FileName`. One or the other. Not both. – Jimi Aug 26 '19 at 22:26