[英]using open file dialog box on a windows form
I wrote a program which reads a csv file, makes some changes and writes to a new csv file. 我编写了一个程序,可以读取csv文件,进行一些更改并写入新的csv文件。
I want the user to be able to select the csv file to be read from their directory using an open file dialog box on a windows form. 我希望用户能够使用Windows窗体上的打开文件对话框从其目录中选择要读取的csv文件。
So far I have been able to write some of the code so that the user can look for the file but I am not sure on how to link the file the user has chosen to the steamreader. 到目前为止,我已经能够编写一些代码,以便用户可以查找文件,但是我不确定如何将用户选择的文件链接到Steamreader。
This is the code to read and write the csv file 这是读写csv文件的代码
try
{
using (StreamWriter sw = new StreamWriter("X:/PublishedSoftware/Data/NEWPYAEGON1PENSION.csv"))
{
using (StreamReader sr = new StreamReader(""))
{
This is the code for the open file dialog box 这是打开文件对话框的代码
private void btnFindAegonFile_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "csv files(*.csv)|*.csv|All files(*.*)|*.*";
openFileDialog1.FileName = "Browse for the AEGON file.";
DialogResult result = openFileDialog1.ShowDialog();
txtFindAegonFile.Text = this.openFileDialog1.FileName;
If you have the file name: 如果您具有文件名:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = this.openFileDialog1.FileName;
...
}
You can read the contents using the stream reader (in the place of ...
): 您可以使用流阅读器(代替
...
)阅读内容:
using (StreamReader sr = new StreamReader(fileName))
Or read the contents directly: 或直接阅读内容:
string input = File.ReadAllText(fileName);
You have to complete the FileOpen Dialog code snippet by passing the file path to StreamWriter, like: 您必须通过将文件路径传递给StreamWriter来完成FileOpen对话框代码片段,例如:
using (StreamWriter sw = new StreamWriter(fileName));
// ... open the file w/StreaWriter
Here is a snippet of how to go from a file name to a StreamReader: 以下是如何从文件名转到StreamReader的代码段:
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (var sr = new StreamReader(fs))
{
...
}
I got it working I used: 我使用了它的工作:
string readfilename = txtFindAegonFile.Text;
try
{
using (StreamReader sr = new StreamReader(readfilename))
using (StreamWriter sw = new StreamWriter("X:/PublishedSoftware/Data/NEWPYAEGON1PENSION.csv"))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.