简体   繁体   English

不支持给定路径的格式。 刚开始使用C#

[英]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. 好的,所以我今天才开始自学C#,终于完全陷入僵局。 I am trying the use a browse option to select a file. 我正在尝试使用浏览选项来选择文件。 The file path will then be displayed in textBox1. 然后,文件路径将显示在textBox1中。 Then I need what is textBox1 to be loaded by clicking the Launch button. 然后,我需要通过单击启动按钮来加载textBox1。

I currently have textBox1.Text set as the location of the file. 我目前将textBox1.Text设置为文件的位置。 When I type \\TestList.xml into the textbox, it goes through fine and does what it is supposed to. 当我在文本框中键入\\ TestList.xml时,它会正常运行并达到预期的效果。 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. 但是,在其他任何时间,例如,如果我键入c:\\ TestList.xml或c:\\ TestList.xml,它只是说它不能使用textBox1.Text格式作为文件位置。 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 Application.StartupPath返回正在运行的exe的路径( 从MSDN获取启动应用程序的可执行文件的路径,不包括可执行文件的名称),因此,如果提供/TestList.xml,它将从Bin中加载文件。

If you give the c:\\TestList.xml , then it appends the path something like this 如果提供c:\\ TestList.xml ,那么它将在路径后添加类似内容

"D:\\urapppath\\bin\\c:\\TestList.xml" , its invalid right... “ D:\\ urapppath \\ bin \\ c:\\ TestList.xml” ,其无效权限...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#不支持给定路径的格式。 UNC路径 - c# The given path's format is not supported. UNC Path 不支持给定路径的格式。 C#Discord机器人 - The given path's format is not supported. c# discord bot System.NotSupportedException : '不支持给定路径的格式。' C# - System.NotSupportedException : 'The format of the given path is not supported.' C# c#-无法备份mysql数据库,错误为“不支持给定路径的格式。” - c# - unable to backup mysql database with error “The given path's format is not supported.” Bot中的MS Bot c#自定义视觉预测端点错误“不支持给定路径的格式。” - MS Bot c# Custom Vision prediction endpoint error in Bot “The given path’s format is not supported.” 不支持给定路径的格式。 - The given path's format is not supported. “不支持给定路径的格式。” - “The given path's format is not supported.” “不支持给定路径的格式。” - “The given path's format is not supported.” 当文件名包含空格时创建文件时,C#异常引发错误{“不支持给定路径的格式。”} - C# Exception when creating a file when file name has spaces throws an error {“The given path's format is not supported.”} 给定路径的格式不受支持C# - The given path's format is not supported C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM