Invalid characters in C # error path

I get an error

Invalid characters on the way

for the code below. Please help me.

response.Clear(); response.Charset = ""; Response.ContentEncoding = System.Text.Encoding.Default; // set the response mime type for excel response.ContentType = "application/vnd.ms-excel"; string sFileName = "Testeeeeee.xls"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + sFileName + "\""); // create a string writer using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // instantiate a datagrid DataGrid dg = new DataGrid(); dg.DataSource = DS.Tables[0]; dg.DataBind(); dg.RenderControl(htw); string sPath = @"E:\CR-12\Test.xls"; File.WriteAllText(sw.ToString(), sPath);// I get the error at this line response.End(); 
+4
source share
5 answers

Parameters are inverted. Do it as follows:

 File.WriteAllText(sPath,sw.ToString()); 
+10
source

You have options for File.WriteAllText . The first is the path, the second is the content. You need

 File.WriteAllText(sPath, sw.ToString()); 
+5
source

Call

 File.WriteAllText(sw.ToString(), sPath); 

has the wrong order of parameters. It should be

 File.WriteAllText(sPath, sw.ToString()); 
+4
source

For other users receiving this error, make sure you use the backslash correctly, as they are also escape characters. This error can lead to one backslash. Use 2 backslashes to correctly output a single escape slash.

Example:

 System.IO.File.WriteAllText("C:\\file.txt", output); 
+2
source

The problem is probably related to security. Your web page will not have access to drive E.

-1
source

All Articles