2014-03-05 63 views
2

這是我的第一個問題,而英文不是我的第一語言,請您全面瞭解我。「指定路徑的格式不受支持」

Basicaly我的程序將數據保存在.js文件中。我使用SaveFileDialog方法設置路徑並使用.FileName設置...以及文件名。下面是我如何做到這一點

private void parcourir_Click(object sender, EventArgs e) 
{ 
    SaveFileDialog exportJSFile = new SaveFileDialog(); 

    // Getting year, month and day of the day to generate the file name 
    DateTime date = DateTime.Today; 

    exportJSFile.FileName = date.Year + "_" + date.Month + "_" + date.Day + "_ct.js"; 
    if (exportJSFile.ShowDialog() == DialogResult.OK) 
    { 
     this.JSfilePath.Text = exportJSFile.FileName; 
    } 
} 

然後我用的StreamWriter在我的文件中寫入數據

// Writing the first block (header) of data into the .js file 
System.IO.StreamWriter objWriterFirstBlock; 
objWriterFirstBlock = new System.IO.StreamWriter(@JSfilePath.ToString()); 
objWriterFirstBlock.Write(firstBlock); 
objWriterFirstBlock.Close(); 

當我調試它,我得到這條線來上述錯誤信息:

objWriterFirstBlock = new System.IO.StreamWriter(@JSfilePath.ToString()); 

我試過同樣的命令,沒有@,相同的結果。當我使用對話框設置路徑名稱時,路徑顯示在文本框中,看起來是正確的。當我赤JSfilePath.ToString()在調試器的值時,它表明類似的路徑:

@JSfilePath = {Text = "C:\\Users\\admin\\Documents\\2014_3_5_ct.js"} 

誰能告訴我什麼是錯的

+0

'@'在標識符前逃脫indentifier,而不是其內容。如果在另一種編程語言中創建的庫中定義的標識符碰巧是C#中的關鍵字,那麼這只是有用的。 '@if =「hello」;'假設變量'if'是一個字符串。 –

回答

6

假設JSfilePathTextBox,看來你是使用TextBox本身的ToString()方法,該方法不會返回您正在查找的內容。

如果將其更改爲JSfilePath.Text這應該修復它爲您提供:

objWriterFirstBlock = new System.IO.StreamWriter(JSfilePath.Text); 
+0

是的,'textbox.ToString()'會返回類似於「」System.Windows.Forms.TextBox,Text:xyz「'的東西。 –

相關問題