C# C语言中的OpenFileDialog#
如何从打开的文件对话框中获得结果(即文件名及其位置) 我的代码:C# C语言中的OpenFileDialog#,c#,winforms,dialog,label,C#,Winforms,Dialog,Label,如何从打开的文件对话框中获得结果(即文件名及其位置) 我的代码: private void selectFileButton_Click( object sender, EventArgs e ) { var selectedFile = selectFileDialog.ShowDialog(); //label name = fileName fileName.Text = //the result from selectedFileDialog } OpenFil
private void selectFileButton_Click( object sender, EventArgs e ) {
var selectedFile = selectFileDialog.ShowDialog();
//label name = fileName
fileName.Text = //the result from selectedFileDialog
}
OpenFileDialog类具有该类的FileName属性 通常,您希望确保用户没有取消该对话框:
using (var selectFileDialog = new OpenFileDialog()) {
if (selectFileDialog.ShowDialog() == DialogResult.OK) {
fileName.Text = selectFileDialog.FileName;
}
}
您可以尝试使用此代码
if(selectFileDialog.ShowDialog() == DialogResult.OK)
{
var result = selectFileDialog.FileName;
if((myStream = selectFileDialog.OpenFile())!= null)
{
// Insert code to read the stream here.
..........
myStream.Close();
}
}
出现了什么问题?我不知道如何从对话框中获取所选文件的名称和位置您是否查看了
FileDialog
对象的属性?
if(selectFileDialog.ShowDialog() == DialogResult.OK)
{
var result = selectFileDialog.FileName;
if((myStream = selectFileDialog.OpenFile())!= null)
{
// Insert code to read the stream here.
..........
myStream.Close();
}
}
private void selectFileButton_Click( object sender, EventArgs e )
{
Stream fileStream = null;
//Update - remove parenthesis
if (selectFileDialog.ShowDialog() == DialogResult.OK && (fileStream = selectFileDialog.OpenFile()) != null)
{
string fileName = selectFileDialog.FileName;
using (fileStream)
{
// TODO
}
}
}