Reading File Error: illegal characters in path in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hi

i had asked a question Highlight and focus selected word in IFRAME using jQuery in ASP.Net

It doesnt work all the time. Please help.

        public void getbookmark()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
            string bmnId = Session["bmId"].ToString();
            string bstxt = Session["bookmark"].ToString();

            SqlCommand cmd = new SqlCommand("select bn.vcFilePath from tblBookNodes bn inner join tbl_bookmarks bk on bn.iModuleId=bk.iNodeId where bn.iModuleId='" + bmnId + "' ", conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                bytes = (byte[])dr["vcFilePath"];
                conn.Close();
            }
            getfile();
        }
        public void getfile()
        {
            string bmnId = Session["bookmark"].ToString();
            string bstxt = Session["bookmarktxt"].ToString();
            string fileName = bmnId.Replace(" ", "_") + ".htm";
            //string returnpath = "File/htmlFile/";
            string strPath = Server.MapPath("~/htmlFile/");
            
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }
            string path = strPath + fileName;
            var doc = new HtmlDocument();
            string html = Encoding.UTF8.GetString(bytes);
            doc.LoadHtml(html);
            StringWriter sw = new StringWriter();
            var hw = new HtmlTextWriter(sw);
            StreamWriter sWriter = new StreamWriter(path);
            sWriter.Write(sw.ToString());
            doc.Save(sWriter);
            sWriter.Close();
            string fileContents = html;
            string searchText = HttpUtility.HtmlEncode(bstxt);
            string newBookmarktag =" <a= '"+bstxt+"'</a>";
            html = html.Replace(bstxt, newBookmarktag);
            //fileContents = fileContents.Replace(elstext, newBookmarktag);
            hfHTML.Value = System.IO.File.ReadAllText(path);
            System.IO.File.WriteAllText(path, hfHTML.Value);
            //Page page = (Page)HttpContext.Current.Handler;
            //MultiView control = ((Cyient_IETM.select)(page)).MultiView1;
            //View View1 = (View)control.FindControl("View1");
            //HtmlControl ctrl = (HtmlControl)View1.FindControl("frmDisplay");
            frmDisplay.Attributes.Add("src", HttpContext.Current.Request.Url + "/.." + "\\htmlFile\\" + fileName);
            //MultiView1.SetActiveView(View1);
        }

newbookmark works fine. I can't use mouse hover on it. If i pass that html to hfHTML it gives error like illegal charecters .

string path = strPath + fileName;
hfHTML.Value = File.ReadAllText(path);

I do get the html file in the path but i am  not able to hover the text.

Update : When write 1st and then read i am able to hover the text but it will not jump to the word. but If i read HTML file it gives error like illegal charecters in path.

System.IO.File.WriteAllText(path, html);
hfHTML.Value = File.ReadAllText(path);
Posted one month ago

File.ReadAllText() excepts the file path you want to read.

From your error message, your file path contains some illegal characters. 

So you need to verify the path. The path you are passing might be wrong.