如何使用openfiledialog控件将文件路径传递给变量? [英] how to pass file path to a variable using openfiledialog control?

查看:30
本文介绍了如何使用openfiledialog控件将文件路径传递给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有这个代码,我用它来以 Windows 形式上传一些东西:

public Form1(){初始化组件();}private void btnLoad_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();System.Windows.Forms.DialogResult dr = ofd.ShowDialog();如果(博士 == DialogResult.OK){userSelectedFilePath = ofd.FileName;}}公共字符串 userSelectedFilePath{得到{ 返回 tbFilePath.Text;}放{tbFilePath.Text = value;}}private void btn_compare_Click(object sender, EventArgs e){字符串 Xml1 = tbFilePath.Text;string Xml2 = System.IO.File.ReadAllText(@"C:");compare.comparison(Xml1, Xml2);}

显然我做错了什么,因为我没有传递我需要的 tbFilePath.Text :string Xml1 = tbFilePath.Text;

这是什么?

解决方案

您可能想要的是比较 2 个文件的内容.
正如siride所说,您的代码没有意义(请参阅他的评论)
将此方法添加到您的类中

私有字符串 FindFile(){OpenFileDialog ofd = new OpenFileDialog();string _xmlPath1 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);System.Windows.Forms.DialogResult dr = ofd.ShowDialog();如果(博士 == DialogResult.OK)返回 d.FileName;别的返回空;}

然后你可以这样做:

private void btn_compare_Click(object sender, EventArgs e){字符串 x1 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);字符串 x2 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);//或者如果你已经有了第二个文件//string x2 = System.IO.File.ReadAllText(@"C:\YourPath\someFileName.xml", Encoding.UTF8);compare.comparison(x1, x2);}

I have this code here which i use in order to upload some stuff in a windows form:

public Form1()
{
    InitializeComponent();
}
private void btnLoad_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.OK)
    {
        userSelectedFilePath = ofd.FileName;
    }
}

public string userSelectedFilePath
{
    get
    { return tbFilePath.Text;
    }
    set
    {tbFilePath.Text = value;
    }
}

private void btn_compare_Click(object sender, EventArgs e)
{
    string Xml1 = tbFilePath.Text;
    string Xml2 = System.IO.File.ReadAllText(@"C:");
    compare.comparison(Xml1, Xml2);

}

Apparently i'm doing something wrong because i'm not passing the tbFilePath.Text which i need when i have: string Xml1 = tbFilePath.Text;

What is it?

解决方案

What you probably want is to compare the contents of 2 files.
As siride said your code does not make sense(see his comment)
Add this method to your class

private string FindFile()
{
   OpenFileDialog ofd = new OpenFileDialog();
   string _xmlPath1 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
   System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
   if (dr == DialogResult.OK)
       return  ofd.FileName;
   else
      return null;

}

And then you can do this:

private void btn_compare_Click(object sender, EventArgs e)
{
    string x1 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);
    string x2 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);
    //Or if you already have the second file
    //string x2 = System.IO.File.ReadAllText(@"C:\YourPath\someFileName.xml", Encoding.UTF8);
    compare.comparison(x1, x2);
}

这篇关于如何使用openfiledialog控件将文件路径传递给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆