简体   繁体   中英

“The given path's format is not supported”

This is my first question and english is not my first language so please be comprehensive with me.

Basicaly my program save data in a .js file. I use SaveFileDialog method to set the path and use .FileName to set the... well file name. Here's how I do it

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

Then I use the StreamWriter to write data in my file

// 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();

When I debug it, I get the above error message coming from this line:

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

I tried the same command without the @, same result. When I use the dialog box to set the path name, the path is displayed in a textbox and it seems right. When I chek the value of JSfilePath.ToString() in the debugger, it shows a path like:

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

Can someone tell me what's wrong

Assuming that JSfilePath is a TextBox , it appears that you are using the ToString() method on the TextBox itself, which will not return what you are looking for.

If you change it to JSfilePath.Text this should fix it for you:

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

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