FileDialog.FileName-Eigenschaft
Ruft eine Zeichenfolge ab, die den im Dateidialogfeld ausgewählten Dateinamen enthält, oder legt diese fest.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Syntax
'Declaration
Public Property FileName As String
'Usage
Dim instance As FileDialog
Dim value As String
value = instance.FileName
instance.FileName = value
public string FileName { get; set; }
public:
property String^ FileName {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_FileName ()
/** @property */
public void set_FileName (String value)
public function get FileName () : String
public function set FileName (value : String)
Eigenschaftenwert
Der Dateiname, der im Dateidialogfeld ausgewählt wurde. Der Standardwert ist eine leere Zeichenfolge ("").
Hinweise
Der Dateiname enthält sowohl den Dateipfad als auch die Erweiterung. Wenn keine Dateien ausgewählt sind, gibt diese Methode eine leere Zeichenfolge ("") zurück.
Wenn diese Eigenschaft von der SaveFileDialog-Klasse verwendet wird, stellt sie die zu speichernde Datei dar. Wird die Eigenschaft von der OpenFileDialog-Klasse verwendet, stellt sie die zu öffnende Datei dar.
Diese Eigenschaft kann nur der Name einer ausgewählten Datei sein. Wenn ein Array mit den Namen aller in einem Dialogfeld mit Mehrfachauswahl ausgewählten Dateien zurückgegeben werden soll, verwenden Sie FileNames.
Beispiel
Im folgenden Codebeispiel wird die Verwendung der RichTextBox.SaveFile-Methode und der RichTextBox.LoadFile-Methode mit Streams veranschaulicht. Außerdem wird das Verwenden der Member FileName, DefaultExt, SaveFileDialog.CreatePrompt und SaveFileDialog.OverwritePrompt dargestellt.
Dies ist ein vollständiges Beispiel, das Sie ausführen können, wenn Sie es in Ihr Projekt kopieren.
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
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();
}
}
}
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
public private:
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()
: Form()
{
userInput = gcnew System::IO::MemoryStream;
this->RichTextBox1 = gcnew System::Windows::Forms::RichTextBox;
this->Button1 = gcnew System::Windows::Forms::Button;
this->RichTextBox2 = gcnew System::Windows::Forms::RichTextBox;
this->Button2 = gcnew System::Windows::Forms::Button;
this->SaveFileDialog1 = gcnew System::Windows::Forms::SaveFileDialog;
this->SuspendLayout();
this->RichTextBox1->Location = System::Drawing::Point( 24, 64 );
this->RichTextBox1->Name = "RichTextBox1";
this->RichTextBox1->TabIndex = 0;
this->RichTextBox1->Text = "Type something here.";
this->Button1->Location = System::Drawing::Point( 96, 16 );
this->Button1->Name = "Button1";
this->Button1->Size = System::Drawing::Size( 96, 24 );
this->Button1->TabIndex = 1;
this->Button1->Text = "Save To Stream";
this->Button1->Click += gcnew System::EventHandler( this, &Form1::Button1_Click );
this->RichTextBox2->Location = 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 = System::Drawing::Point( 104, 200 );
this->Button2->Name = "Button2";
this->Button2->Size = System::Drawing::Size( 88, 32 );
this->Button2->TabIndex = 4;
this->Button2->Text = "Save Stream To File";
this->Button2->Click += gcnew System::EventHandler( this, &Form1::Button2_Click );
this->ClientSize = 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 );
}
// Declare a new memory stream.
System::IO::MemoryStream^ userInput;
private:
// Save the content of RichTextBox1 to the memory stream,
// appending a LineFeed character.
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.
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.
System::Windows::Forms::DialogResult result = SaveFileDialog1->ShowDialog();
System::IO::Stream^ fileStream;
if ( result == ::DialogResult::OK )
{
fileStream = SaveFileDialog1->OpenFile();
userInput->Position = 0;
userInput->WriteTo( fileStream );
fileStream->Close();
}
}
};
int main()
{
Application::Run( gcnew Form1 );
}
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
.NET Framework-Sicherheit
- FileIOPermission zum Abrufen oder Festlegen des Dateinamens. Zugeordnete Enumeration: PermissionState.Unrestricted.
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
FileDialog-Klasse
FileDialog-Member
System.Windows.Forms-Namespace
FileNames
SaveFileDialog
OpenFileDialog