好的,所以我今天剛開始自學C#,終於完全被卡住了。我正在嘗試使用瀏覽選項來選擇文件。文件路徑將顯示在textBox1中。然後,我需要通過單擊啓動按鈕來加載textBox1。不支持給定路徑的格式。剛開始使用C#
我目前已將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";
}
}
}
@asad:你的忍者編輯將使它很難讓人理解OP做錯了什麼。 – Dave 2010-02-22 06:51:38
希望現在好一點 – 2010-02-22 06:55:27
在這種情況下,一個好的調試策略是檢查實際傳遞給給你的問題的方法。在你的情況下,在readXml()行設置一個斷點並檢查Application.StartupPath + textBox1.Text的值 或者,將代碼更改爲: string whatIsWrong = Application.StartupPath + textBox1.Text; MessageBox.Show(whatIsWrong); xmldata.DataSet.ReadXml(whatIsWrong); 您會看到@Ramesh顯示的無效路徑。 – JeffH 2010-03-29 20:33:43