[英]The given path's format is not supported. Just started using C#
好的,所以我今天才开始自学C#,终于完全陷入僵局。 我正在尝试使用浏览选项来选择文件。 然后,文件路径将显示在textBox1中。 然后,我需要通过单击启动按钮来加载textBox1。
我目前将textBox1.Text设置为文件的位置。 当我在文本框中键入\\ TestList.xml时,它会正常运行并达到预期的效果。 但是,在其他任何时间,例如,如果我键入c:\\ TestList.xml或c:\\ TestList.xml,它只是说它不能使用textBox1.Text格式作为文件位置。 任何想法如何解决这个问题? 这是代码。 我在导致问题的行旁边添加了一些破折号。 非常感谢您对此的任何帮助。
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返回正在运行的exe的路径( 从MSDN获取启动应用程序的可执行文件的路径,不包括可执行文件的名称),因此,如果提供/TestList.xml,它将从Bin中加载文件。
如果提供c:\\ TestList.xml ,那么它将在路径后添加类似内容
“ D:\\ urapppath \\ bin \\ c:\\ TestList.xml” ,其无效权限...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.