FileDialog中选择的文件的写入路径



我做了一个WindowsForms程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace FormChooseFolder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = @"C:",
Title = "Browse Text Files",
CheckFileExists = true,
};

this.openFileDialog1.Multiselect = true;
this.openFileDialog1.Title = "Select Files";
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
TextWriter txt = new StreamWriter("C:\test.txt");
txt.Write(dr);
txt.Close();
}
}
}
}

应该将所选文件的路径写入test.txt但是它写的是OK.有没有办法让它像这样显示所选文件的路径?

不将DialogResult的对象存储到test.txt,而将用户喜欢的FileName存储到CC_4,

DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
TextWriter txt = new StreamWriter("C:\test.txt");
txt.Write(dr.FileName);  //Use FileName property to get entire file path
txt.Close();
}

MSDN documentation: FileDialog。文件名属性

获取或设置一个字符串,其中包含文件中选择的文件名对话框。

相关内容

  • 没有找到相关文章

最新更新