0

I'm having problem to completely figure out File.CreatTextM Method. What i'm trying to do is make a button that would prompt user to imput a text file name, prompt again to enter name, and finally create the text file in predestined location.

As far as i know in "Sring path =" you must predetermine name in the code, is there a command that opens a new window (or something) for user to input, so that i could store it to a variable and use in "Sring path ="

Or is there another approach for this?

xsairo
  • 1
  • 3

4 Answers4

4

if you are using windows form, then you can use saveFileDialog or for wpf win32 saveFileDialog, then you can get file name from this object. Use that filename to save your text.

Naresh
  • 374
  • 1
  • 4
1

Unfortunately there is no InputBox in C#, other than if you are creating a custom one. Best solution is to place some textbox control in your form for same purpose. Then you can get their values in Button Click event and perform the logic.

Rahul
  • 76,197
  • 13
  • 71
  • 125
1

You could create another Form with the necessary buttons and textboxes then show the form to the user, as referenced here : Original Post

Community
  • 1
  • 1
Tom Gothorp
  • 93
  • 1
  • 8
1

use savefile dialog. this code is from msdn

 private void button2_Click(object sender, System.EventArgs e)
 {
  // Displays a SaveFileDialog so the user can save the Image
  // assigned to Button2.
  SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif        Image|*.gif";
   saveFileDialog1.Title = "Save an Image File";
  saveFileDialog1.ShowDialog();

   // If the file name is not an empty string open it for saving.
  if(saveFileDialog1.FileName != "")
  {
   // Saves the Image via a FileStream created by the OpenFile method.
   System.IO.FileStream fs = 
     (System.IO.FileStream)saveFileDialog1.OpenFile();
  // Saves the Image in the appropriate ImageFormat based upon the
  // File type selected in the dialog box.
  // NOTE that the FilterIndex property is one-based.
  switch(saveFileDialog1.FilterIndex)
  {
     case 1 : 
     this.button2.Image.Save(fs, 
        System.Drawing.Imaging.ImageFormat.Jpeg);
     break;

     case 2 : 
     this.button2.Image.Save(fs, 
        System.Drawing.Imaging.ImageFormat.Bmp);
     break;

     case 3 : 
     this.button2.Image.Save(fs, 
        System.Drawing.Imaging.ImageFormat.Gif);
     break;
    }

    fs.Close();
   }
  }
dada
  • 273
  • 2
  • 14