Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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)

public string FileName { get; set; }
/** @property */
public String get_FileName ()

/** @property */
public void set_FileName (String value)

public function get FileName () : String

public function set FileName (value : String)

Property Value

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 System.Windows.Forms;



public class Form1:
	System.Windows.Forms.Form

{
	
	internal System.Windows.Forms.RichTextBox RichTextBox1;
	internal System.Windows.Forms.Button Button1;
	internal System.Windows.Forms.RichTextBox RichTextBox2;
	internal System.Windows.Forms.Button Button2;
	internal System.Windows.Forms.SaveFileDialog SaveFileDialog1;

	public Form1() : base()
	{   
		
		this.RichTextBox1 = new System.Windows.Forms.RichTextBox();
		this.Button1 = new System.Windows.Forms.Button();
		this.RichTextBox2 = new System.Windows.Forms.RichTextBox();
		this.Button2 = new System.Windows.Forms.Button();
		this.SaveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
		this.SuspendLayout();
		this.RichTextBox1.Location = new System.Drawing.Point(24, 64);
		this.RichTextBox1.Name = "RichTextBox1";
		this.RichTextBox1.TabIndex = 0;
		this.RichTextBox1.Text = "Type something here.";
		this.Button1.Location = new System.Drawing.Point(96, 16);
		this.Button1.Name = "Button1";
		this.Button1.Size = new System.Drawing.Size(96, 24);
		this.Button1.TabIndex = 1;
		this.Button1.Text = "Save To Stream";
		this.Button1.Click += new System.EventHandler(Button1_Click);
		this.RichTextBox2.Location = new System.Drawing.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 = new System.Drawing.Point(104, 200);
		this.Button2.Name = "Button2";
		this.Button2.Size = new System.Drawing.Size(88, 32);
		this.Button2.TabIndex = 4;
		this.Button2.Text = "Save Stream To File";
		this.Button2.Click += new System.EventHandler(Button2_Click);
		this.ClientSize = new System.Drawing.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);

	}


	public static void Main()
	{
		Application.Run(new Form1());
	}


	// Declare a new memory stream.
	System.IO.MemoryStream userInput = new System.IO.MemoryStream();

	// Save the content of RichTextBox1 to the memory stream, 
	// appending a LineFeed character.  
	private void Button1_Click(System.Object sender, 
		System.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.
	private void Button2_Click(System.Object sender, System.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 drive C.
		SaveFileDialog1.FileName = "myText";
		SaveFileDialog1.DefaultExt = "txt";
		SaveFileDialog1.Filter = "Text files (*.txt)|*.txt";
		SaveFileDialog1.InitialDirectory = "c:\\";

		// Call ShowDialog and check for a return value of DialogResult.OK,
		// which indicates that the file was saved. 
		DialogResult result = SaveFileDialog1.ShowDialog();
		System.IO.Stream fileStream;

		if (result == DialogResult.OK)

			// Open the file, copy the contents of memoryStream to fileStream,
			// and close fileStream. Set the memoryStream.Position value to 0 to 
			// copy the entire stream. 
		{
			fileStream = SaveFileDialog1.OpenFile();
			userInput.Position = 0;
			userInput.WriteTo(fileStream);
			fileStream.Close();
		}
	}
}

import System.Windows.Forms.*;

public class Form1 extends System.Windows.Forms.Form
{
    System.Windows.Forms.RichTextBox richTextBox1;
    System.Windows.Forms.Button button1;
    System.Windows.Forms.RichTextBox richTextBox2;
    System.Windows.Forms.Button button2;
    System.Windows.Forms.SaveFileDialog saveFileDialog1;

    public Form1()
    {
        super();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.button1 = new System.Windows.Forms.Button();
        this.richTextBox2 = new System.Windows.Forms.RichTextBox();
        this.button2 = new System.Windows.Forms.Button();
        this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
        this.SuspendLayout();
        this.richTextBox1.set_Location(new System.Drawing.Point(24, 64));
        this.richTextBox1.set_Name("RichTextBox1");
        this.richTextBox1.set_TabIndex(0);
        this.richTextBox1.set_Text("Type something here.");
        this.button1.set_Location(new System.Drawing.Point(96, 16));
        this.button1.set_Name("Button1");
        this.button1.set_Size(new System.Drawing.Size(96, 24));
        this.button1.set_TabIndex(1);
        this.button1.set_Text("Save To Stream");
        this.button1.add_Click(new System.EventHandler(button1_Click));
        this.richTextBox2.set_Location(new System.Drawing.Point(152, 64));
        this.richTextBox2.set_Name("RichTextBox2");
        this.richTextBox2.set_TabIndex(3);
        this.richTextBox2.set_Text("It will be added to the stream "
            + "and appear here.");
        this.button2.set_Location(new System.Drawing.Point(104, 200));
        this.button2.set_Name("Button2");
        this.button2.set_Size(new System.Drawing.Size(88, 32));
        this.button2.set_TabIndex(4);
        this.button2.set_Text("Save Stream To File");
        this.button2.add_Click(new System.EventHandler(button2_Click));
        this.set_ClientSize(new System.Drawing.Size(292, 266));
        this.get_Controls().Add(this.button2);
        this.get_Controls().Add(this.richTextBox2);
        this.get_Controls().Add(this.button1);
        this.get_Controls().Add(this.richTextBox1);
        this.set_Name("Form1");
        this.set_Text("Form1");
        this.ResumeLayout(false);
    } //Form1

    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

    // Declare a new memory stream.
    private System.IO.MemoryStream userInput = new System.IO.MemoryStream();

    // Save the content of RichTextBox1 to the memory stream, 
    // appending a LineFeed character.  
    private void button1_Click(Object sender, System.EventArgs e)
    {
        richTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText);
        userInput.WriteByte(System.Convert.ToByte(13));
        // Display the entire contents of the stream,
        // by setting its position to 0, to RichTextBox2.
        userInput.set_Position(0);
        richTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText);
    } //button1_Click

    // Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    private void button2_Click(Object sender, System.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.set_CreatePrompt(true);
        saveFileDialog1.set_OverwritePrompt(true);
        // Set the file name to myText.txt, set the type filter
        // to text files, and set the initial directory to drive C.
        saveFileDialog1.set_FileName("myText");
        saveFileDialog1.set_DefaultExt("txt");
        saveFileDialog1.set_Filter("Text files (*.txt)|*.txt");
        saveFileDialog1.set_InitialDirectory("c:\\");
        // Call ShowDialog and check for a return value of DialogResult.OK,
        // which indicates that the file was saved. 
        DialogResult result = saveFileDialog1.ShowDialog();
        System.IO.Stream fileStream;

        if (result.Equals(DialogResult.OK)) {
            // Open the file, copy the contents of memoryStream to fileStream,
            // and close fileStream. Set the memoryStream.Position value to 0 to 
            // copy the entire stream. 
            fileStream = saveFileDialog1.OpenFile();
            userInput.set_Position(0);
            userInput.WriteTo(fileStream);
            fileStream.Close();
        }
    } //button2_Click
} //Form1

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.