-2

I wrote this code for a "save" button

        {
            File.WriteAllText(ofd.FileName, textBox1.Text);
        }

And I want to do a "save as" button, to create a file by my app but idk how to do it, someone know how to ? Or just give me a doc, thx.

kichi
  • 11
  • 1
  • How does this code save anything?? It seems to read **from** a file, not save **to** it – ADyson Dec 03 '20 at 20:24
  • woops, wrong code ! I'll edit it – kichi Dec 03 '20 at 20:25
  • 1
    Use the SaveFileDialog. Once the user browsed the place of the new file it can be obtained by saveFileDialog.FileName. Put this value to the File.WriteAllText method. – Mitulát báti Dec 03 '20 at 20:30
  • Take a look at [SaveFileDialog In C# (C# Corner)](https://www.c-sharpcorner.com/UploadFile/mahesh/savefiledialog-in-C-Sharp/). I hope this can help you to enjoy C# coding: [How do I improve my knowledge in C#](http://www.ordisoftware.com/files/stack-overflow/CsharpBegin.htm) –  Dec 03 '20 at 20:34
  • thx ! I'm new here, why there is "-2" to my post ? – kichi Dec 03 '20 at 20:36
  • @kichi Because your question shows very little research effort which is one of the reasons to downvote (if you hover over the downvote button it will show a tooltip). A quick google search for "winforms save as" pulls up the duplicate question I link below – tnw Dec 03 '20 at 20:44
  • And also probably because you said "give me a doc", but people here resent being seen as a free do-my-research service. And also because, as I pointed out, the question initially did not make sense. And not everyone sticks around to see if you updated it or not. It's a good idea to proof read your question carefully before you post it, as most voting and commenting tends to happen in the first few minutes – ADyson Dec 03 '20 at 20:49
  • oh, I see. Thx ! I'll be carefuly next time ! – kichi Dec 03 '20 at 20:52

1 Answers1

0

To prompt the user with a "Save As" file prompt:

Create a button on your form. Set the Click event to btnSaveAsClick().

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
        {
            InitializeComponent();
        }

    private void btnSaveAs_Click(Object sender, EventArgs e)
    {
        String path = GetSaveFileName("usage.csv");
        if(String.IsNullOrEmpty(path)) return;
        //Write your file here using 'path'
    }

    private String GetSaveFileName(String SuggestedFileName)
    {
        SaveFileDialog ofd = new SaveFileDialog();
        ofd.FileName = SuggestedFileName;
        ofd.DefaultExt = "csv";
        ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        return ofd.ShowDialog()==DialogResult.OK ? ofd.FileName : "";
    }
}

Please remember to click the green checkmark if this solves your issue.

Mr_Engineer
  • 256
  • 1
  • 6
  • It's not create the document on my desktop, why ? – kichi Dec 03 '20 at 20:50
  • @kichi You'll have to show your code for us to help further – Mr_Engineer Dec 03 '20 at 20:56
  • OpenFileDialog ofd = new OpenFileDialog(); private void button4_Click(object sender, EventArgs e) { ofd.Filter = "Text | *.txt"; if(ofd.ShowDialog() == DialogResult.OK) { textBox1.Text = File.ReadAllText(ofd.FileName); } } private void button5_Click(object sender, EventArgs e) { File.WriteAllText(ofd.FileName, textBox1.Text); } – kichi Dec 03 '20 at 21:00
  • Instead of textBox1.Text, use a const string e.g. "Testing" to verify that File.WriteAllText() is working. – Mr_Engineer Dec 03 '20 at 21:24
  • @kichi: The SaveFileDialog DOES NOT SAVE anything. It returns you Ok, if the user selected a file, and you can optain the filename from the instance of the object - as you see in the "GetSaveFileName" method in the last line. – nabuchodonossor Dec 04 '20 at 11:56