FileDialog.FileName 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个包含在文件对话框中选定的文件名的字符串。
public:
property System::String ^ FileName { System::String ^ get(); void set(System::String ^ value); };
public string FileName { get; set; }
member this.FileName : string with get, set
Public Property FileName As String
属性值
对话框中选择的文件名。 默认值为空字符串 ("")。
示例
下面的代码示例演示如何将 RichTextBox.SaveFile 和 RichTextBox.LoadFile 方法与流配合使用。 它还演示了如何使用 FileName、 DefaultExt成员 SaveFileDialog.CreatePrompt和 SaveFileDialog.OverwritePrompt 成员。
这是一个完整的示例,可在将它复制到项目时运行。
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;
public:
Form1()
: 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->SuspendLayout();
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;
private:
// 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 =
Environment->GetFolderPath(Environment::SpecialFolder::MyDocuments);
// 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 );
fileStream->Close();
}
}
};
int main()
{
Application::Run( gcnew Form1 );
}
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
public partial class Form1: Form
{
internal RichTextBox RichTextBox1;
internal Button Button1;
internal RichTextBox RichTextBox2;
internal Button Button2;
internal SaveFileDialog SaveFileDialog1;
public Form1() : base()
{
this.RichTextBox1 = new RichTextBox();
this.Button1 = new Button();
this.RichTextBox2 = new RichTextBox();
this.Button2 = new Button();
this.SaveFileDialog1 = new SaveFileDialog();
this.SuspendLayout();
this.RichTextBox1.Location = new Point(24, 64);
this.RichTextBox1.Name = "RichTextBox1";
this.RichTextBox1.TabIndex = 0;
this.RichTextBox1.Text = "Type something here.";
this.Button1.Location = new Point(96, 16);
this.Button1.Name = "Button1";
this.Button1.Size = new Size(96, 24);
this.Button1.TabIndex = 1;
this.Button1.Text = "Save To Stream";
this.Button1.Click += new EventHandler(Button1_Click);
this.RichTextBox2.Location = new 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 Point(104, 200);
this.Button2.Name = "Button2";
this.Button2.Size = new Size(88, 32);
this.Button2.TabIndex = 4;
this.Button2.Text = "Save Stream To File";
this.Button2.Click += new EventHandler(Button2_Click);
this.ClientSize = new 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.
MemoryStream userInput = new MemoryStream();
// Save the content of RichTextBox1 to the memory stream,
// appending a LineFeed character.
private 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.
private 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 =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// 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)
{
// 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();
}
}
}
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Partial Public Class Form1
Inherits Form
Friend WithEvents RichTextBox1 As RichTextBox
Friend WithEvents Button1 As Button
Friend WithEvents RichTextBox2 As RichTextBox
Friend WithEvents Button2 As Button
Friend WithEvents SaveFileDialog1 As SaveFileDialog
Public Sub New()
MyBase.New()
Me.RichTextBox1 = New RichTextBox
Me.Button1 = New Button
Me.RichTextBox2 = New RichTextBox
Me.Button2 = New Button
Me.SaveFileDialog1 = New SaveFileDialog
Me.SuspendLayout()
Me.RichTextBox1.Location = New Point(24, 64)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "Type something here."
Me.Button1.Location = New Point(96, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New Size(96, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Save To Stream"
Me.RichTextBox2.Location = New 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 Point(104, 200)
Me.Button2.Name = "Button2"
Me.Button2.Size = New Size(88, 32)
Me.Button2.TabIndex = 4
Me.Button2.Text = "Save Stream To File"
Me.ClientSize = New 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 MemoryStream
' Save the content of RichTextBox1 to the memory stream, appending
'a LineFeed character.
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As 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 Object, _
ByVal e As 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 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 = _
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
' 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 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
注解
文件名包括文件路径和扩展名。 如果未选择任何文件,此方法将返回空字符串 (“”) 。
从 SaveFileDialog 类使用时,此属性表示要保存的文件;当从 OpenFileDialog 类中使用时,它表示要打开的文件。
此属性只能是一个所选文件的名称。 如果要返回包含多选对话框中所有选定文件的名称的数组,请使用 FileNames。