0

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?

11
  • The code in your btn_compare_Click function doesn't make sense. Xml1 contains a path to a file and Xml2 contains the contents of a file, except that file is C:, which is a directory. No way this can work. Are you trying to extract the contents of two different files and compare them? Is that how your comparison() method works? Commented Sep 4, 2014 at 13:24
  • Also, you can help yourself by either using the debugging, or putting Console.WriteLine() statements after assigning variables to see what they contain. You might find that they contain the right thing. Commented Sep 4, 2014 at 13:24
  • Oh, i didn't know that, i'm just trying to pass the path of the file i select Commented Sep 4, 2014 at 13:25
  • You code seems to be correct, but have you really selected file in your dialog? :) It happens sometimes Commented Sep 4, 2014 at 13:25
  • @MAL: then what's going on with the ReadAllText(@"C:")? That line doesn't make sense. Commented Sep 4, 2014 at 13:26

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.