tags:

views:

572

answers:

2

Ok, so I just started teaching myself C# today, and I have finally gotten completely stuck. I am trying the use a browse option to select a file. The file path will then be displayed in textBox1. Then I need what is textBox1 to be loaded by clicking the Launch button.

I currently have textBox1.Text set as the location of the file. When I type \TestList.xml into the textbox, it goes through fine and does what it is supposed to. Any other time however, like if I typed c:\TestList.xml or c:\TestList.xml it just says that it cant use the textBox1.Text format as a file location. Any idea how to fix this? here is the code. I added a bunch of dashes next to the line that is causing the problem. Thank you very much for any help with this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Combined
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fdlg.FileName;
            } 
        }

        private void button2_Click(object sender, EventArgs e)
        {                          
                XmlDataDocument xmldata = new XmlDataDocument();

            // causing problem
                xmldata.DataSet.ReadXml(Application.StartupPath + textBox1.Text);

                dataGridView1.DataSource = xmldata.DataSet;
                dataGridView1.DataMember = "Unit";  
        }
    }
}
+1  A: 

Your error is that you have typed in an absolute path, but it is then appended to another absolute path.

Dave
No one can see your problematic code now, but if you set a string variable to what you want to open (startup path + filename) you will realize your error if you print it to the console. You would end up with a filename like c:\program filesc:\test.xml which is of course invalid.
Dave
A: 

Application.StartupPath return the path of your running exe (Gets the path for the executable file that started the application, not including the executable name, from MSDN), so if you give /TestList.xml it loads the file from the Bin

If you give the c:\TestList.xml , then it appends the path something like this

"D:\urapppath\bin\c:\TestList.xml", its invalid right...

Ramesh Vel