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)

'Declaration
Public Property FileName As String
'Usage
Dim instance As FileDialog
Dim value As String

value = instance.FileName

instance.FileName = value
/** @property */
public String get_FileName ()

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

public function get FileName () : String

public function set FileName (value : String)

Not applicable.

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.

Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents RichTextBox2 As System.Windows.Forms.RichTextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog

    Public Sub New()
        MyBase.New()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.RichTextBox2 = New System.Windows.Forms.RichTextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.SuspendLayout()
        Me.RichTextBox1.Location = New System.Drawing.Point(24, 64)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = "Type something here."
        Me.Button1.Location = New System.Drawing.Point(96, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Save To Stream"
        Me.RichTextBox2.Location = New System.Drawing.Point(152, 64)
        Me.RichTextBox2.Name = "RichTextBox2"
        Me.RichTextBox2.TabIndex = 3
        Me.RichTextBox2.Text = "It will be added to the stream and appear here."
        Me.Button2.Location = New System.Drawing.Point(104, 200)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 32)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Save Stream To File"
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.RichTextBox2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.RichTextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub


    ' Declare a new memory stream.
    Dim userInput As New System.IO.MemoryStream

    ' Save the content of RichTextBox1 to the memory stream, appending
    'a LineFeed character.  
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        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)
    End Sub


    ' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click

        ' 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. 
        Dim result As DialogResult = SaveFileDialog1.ShowDialog()
        Dim fileStream As System.IO.Stream

        If (result = DialogResult.OK) Then

            ' 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()
        End If
    End Sub

End Class

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft