I am trying to read a text file that is currently in my project resources and display it on my windows application pop up. This is what I got so far
private void MyWindow_Load(object sender, EventArgs e)
{
string myTxt = System.IO.File.ReadAllText("myText.txt");Console.WriteLine(myTxt); }
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e){
Environment.Exit(1);
}
VulpesPosted Oct 28, 2014, 7:52 PM
MarcPosted Oct 28, 2014, 9:07 AM
string path = "this.xml";
string text = System.IO.File.ReadAllText(path);
Console.WriteLine(text);
I am just practicing on a console application now and not on the windows form as before.
MarcPosted Oct 22, 2014, 2:07 PM
VulpesPosted Oct 22, 2014, 11:24 AM
As I said earlier, it must be finding the file OK, as no exception is being thrown.
MarcPosted Oct 22, 2014, 11:16 AM
VulpesPosted Oct 22, 2014, 11:06 AM
It must be finding the file or else there'd be an exception.
If you open the file with a text editor such as notepad, is there definitely some content?
As an alternative to displaying the file in a MessageBox, you could try displaying it directly in the RTB though DON'T put this code in the TextChanged event handler because it might cause an infinite loop:
MarcPosted Oct 22, 2014, 10:56 AM
VulpesPosted Oct 22, 2014, 10:47 AM
Is the MessageBox just empty or is there an exception?
MarcPosted Oct 22, 2014, 10:30 AM
VulpesPosted Oct 22, 2014, 10:04 AM
MarcPosted Oct 22, 2014, 9:51 AM
private void richTextBox1_textChanged(object sender, eventArgs e)
{
string text = System.IO.File.ReadAllText("myText.txt");
MessageBox.Show(text);
}
VulpesPosted Oct 22, 2014, 9:43 AM
// ....
However, if you need to parse the XML file then that's a different matter and the .NET framework has various tools to enable you to do that.
Wim SturkenboomPosted Oct 22, 2014, 9:37 AM
string path=getpath(); /* for you to get the path */
if(!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
path += Path.DirectorySeparatorChar.ToString();
}
path += textfile;
Wim SturkenboomPosted Oct 22, 2014, 9:31 AM
MarcPosted Oct 22, 2014, 7:57 AM
Actually is there a way I can read an xml file in a dialogbox?
VulpesPosted Oct 21, 2014, 1:10 PM
However, MessageBox.Show(myTxt) should work if it's a valid file path and there's anything displayable in it.