简体   繁体   中英

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

您的错误是您输入了绝对路径,但随后将其附加到另一个绝对路径。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM