如何使用openfiledialog控件将文件路径传递给变量?

编程入门 行业动态 更新时间:2024-10-26 17:23:49
本文介绍了如何使用openfiledialog控件将文件路径传递给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在这里有这个代码,我用它来以 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:");compareparison(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);compareparison(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:");
    compareparison(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);
    compareparison(x1, x2);
}

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

更多推荐

[db:关键词]

本文发布于:2023-04-27 12:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   控件   变量   路径   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!