adam
adam

Reputation: 1

The given path's format is not supported. Just started using C#

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";  
        }
    }
}

Upvotes: 0

Views: 5749

Answers (2)

RameshVel
RameshVel

Reputation: 65867

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...

Upvotes: 1

Dave
Dave

Reputation: 15016

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

Upvotes: 1

Related Questions