Export (0) Print
Expand All

FileDialog::FileName Property

Gets or sets a string containing the file name selected in the file dialog box.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

property String^ FileName {
	String^ get ();
	void set (String^ value);

Property Value

Type: System::String
The file name selected in the file dialog box. The default value is an empty string ("").

The file name includes both the file path and the extension. If no files are selected, this method returns an empty string ("").

When used from the SaveFileDialog class, this property represents the file being saved; when used from the OpenFileDialog class, it represents the file being opened.

This property can only be the name of one selected file. If you want to return an array containing the names of all selected files in a multiple-selection dialog box, use FileNames.

The following code example demonstrates using the RichTextBox::SaveFile and RichTextBox::LoadFile methods with streams. It also demonstrates using the FileName, DefaultExt, SaveFileDialog::CreatePrompt, and SaveFileDialog::OverwritePrompt members.

This is a complete example that is ready to run when you copy it to your project.

using namespace System;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Windows::Forms;

public ref class Form1: public Form
public private:
   RichTextBox^ RichTextBox1;
   Button^ Button1;
   RichTextBox^ RichTextBox2;
   Button^ Button2;
   SaveFileDialog^ SaveFileDialog1;

      : Form()
      userInput = gcnew MemoryStream;
      this->RichTextBox1 = gcnew RichTextBox;
      this->Button1 = gcnew Button;
      this->RichTextBox2 = gcnew RichTextBox;
      this->Button2 = gcnew Button;
      this->SaveFileDialog1 = gcnew SaveFileDialog;
      this->RichTextBox1->Location = Point( 24, 64 );
      this->RichTextBox1->Name = "RichTextBox1";
      this->RichTextBox1->TabIndex = 0;
      this->RichTextBox1->Text = "Type something here.";
      this->Button1->Location = Point( 96, 16 );
      this->Button1->Name = "Button1";
      this->Button1->Size = Size( 96, 24 );
      this->Button1->TabIndex = 1;
      this->Button1->Text = "Save To Stream";
      this->Button1->Click += 
		  gcnew EventHandler( this, &Form1::Button1_Click );
      this->RichTextBox2->Location = Point( 152, 64 );
      this->RichTextBox2->Name = "RichTextBox2";
      this->RichTextBox2->TabIndex = 3;
      this->RichTextBox2->Text = "It will be added to the stream " 
      "and appear here.";
      this->Button2->Location = Point( 104, 200 );
      this->Button2->Name = "Button2";
      this->Button2->Size = Size( 88, 32 );
      this->Button2->TabIndex = 4;
      this->Button2->Text = "Save Stream To File";
      this->Button2->Click += 
		  gcnew EventHandler( this, &Form1::Button2_Click );
      this->ClientSize = Size( 292, 266 );
      this->Controls->Add( this->Button2 );
      this->Controls->Add( this->RichTextBox2 );
      this->Controls->Add( this->Button1 );
      this->Controls->Add( this->RichTextBox1 );
      this->Name = "Form1";
      this->Text = "Form1";
      this->ResumeLayout( false );

   // Declare a new memory stream.
   MemoryStream^ userInput;


   // Save the content of RichTextBox1 to the memory stream,  
   // appending a LineFeed character.   
   void Button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
      RichTextBox1->SaveFile( userInput, RichTextBoxStreamType::PlainText );
      userInput->WriteByte( 13 );

      // Display the entire contents of the stream, 
      // by setting its position to 0, to RichTextBox2.
      userInput->Position = 0;
      RichTextBox2->LoadFile( userInput, RichTextBoxStreamType::PlainText );

   // Shows the use of a SaveFileDialog to save a MemoryStream to a file. 
   void Button2_Click( Object^ /*sender*/, EventArgs^ /*e*/ )

      // Set the properties on SaveFileDialog1 so the user is  
      // prompted to create the file if it doesn't exist  
      // or overwrite the file if it does exist.
      SaveFileDialog1->CreatePrompt = true;
      SaveFileDialog1->OverwritePrompt = true;

      // Set the file name to myText.txt, set the type filter 
      // to text files, and set the initial directory to the 
	  // MyDocuments folder.
      SaveFileDialog1->FileName = "myText";
	  // DefaultExt is only used when "All files" is selected from 
      // the filter box and no extension is specified by the user.
      SaveFileDialog1->DefaultExt = "txt";
      SaveFileDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
      SaveFileDialog1->InitialDirectory = 

      // Call ShowDialog and check for a return value of DialogResult.OK, 
      // which indicates that the file was saved. 
      DialogResult result = SaveFileDialog1->ShowDialog();
      Stream^ fileStream;
      if ( result == DialogResult::OK )
         fileStream = SaveFileDialog1->OpenFile();
         userInput->Position = 0;
         userInput->WriteTo( fileStream );

int main()
   Application::Run( gcnew Form1 );
The following code example demonstrates using the 
RichTextBox.SaveFile and RichTextBox.LoadFile methods with streams.
It also demonstrates using the FileDialog.FileName, 
SaveFileDialog.CreatePrompt, SaveFileDialog.OverwritePrompt, and 
TextBox.Click members. This is a complete example that is ready to run when you copy it to your project.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft