我有一个html文件,文件名index.html,index里的body有两个属性,onselectstart、oncontextmenu,初始没有任何设定,现在我想通过C#编写代码,设置body里这两个属性的值,用以达到html不能复制和屏蔽右键的功能。
暂时我想到的思路是通过写入文件的形式实现,但是如何定位到body这个位置,然后在这里写入我想写的东西,这里的代码自己写不出来,请高手帮帮忙,需要如何写,最好能有简单代码或完全代码。另外如果有其他简单方式可以实现这个功能希望能不吝赐教,非常感谢。

解决方案 »

  1.   

    关注一下,c# 如何驱动 html页面的!!
      

  2.   

    新的heml文件内容=System.Text.RegularExpressions.Regex.Replace("html文件的内容", @"<body[^>]*>", "<body onxxx='' onxxx=''>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
      

  3.   

    aspx文件
    <body runat="server" mybody">
    ....
    cs文件
    mybody.attribuate.add....
      

  4.   

    <body runat="server" id="mybody">
      

  5.   

    请问这个新的html文件内容是什么意思?
      

  6.   

    我在把我的意思表达的清楚一点,
    /// <summary>
            ///将Word文档转换成Html文件
            /// </summary>
            /// <param name="docpath"> Word文件所在完整路径</param>
            /// <param name="savepath">生成后Html保存路径</param>
            public static void ConvertWordToHtml(string docpath, string savepath)
            {
                try
                {
                    // 1、指定原文件和目标文件
                    object Source = docpath;
                    string SaveHtmlPath = savepath + "\\index.html";
                    object Target = SaveHtmlPath;
                    // 2、缺省参数  //为了保险,只读方式打开
                    object Unknown = Type.Missing;
                    object readOnly = true;
                    object visible = false;
                    // 3、打开doc文件
                    Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
                    Microsoft.Office.Interop.Word.Document doc = newApp.Documents.Open(ref Source, ref Unknown,
                         ref readOnly, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref visible, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                    // 4、指定另存为格式(HTML) 
                    Type docType = doc.GetType();
                    
                    object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;
                            doc.SaveAs(ref Target, ref format,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown);                // 5、关闭文档和Word程序
                    doc.Close(ref Unknown, ref Unknown, ref Unknown);
                    newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
                }
                // 处理异常
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.Message);
                }
            }上面的代码是我把一个word文件转化成一个名称为index.html的文件的方法,现在我想在这个转化过程顺便向html中的body中写入  oncontextmenu="return false" onselectstart="return false" 这个,实现html的复制屏蔽
      

  7.   

    StreamReader sr= new .......
    string html = sr.ReadToEnd();
    sr.close()
    html=System.Text.RegularExpressions.Regex.Replace(html, @"<body[^>]*>", "<body onxxx='' onxxx=''>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    StreamWriter sw = new........
    sw.Write(html);
    sw.close();明白了?
      

  8.   

    问个弱弱的问题,这个怎么知道操作的是我的index.html文档呢?
      

  9.   

    StreamReader和StreamWriter的方法参数里有设置Encoding
      

  10.   

    谢谢Roy99,解决了,非常感谢,解决代码如下:
     /// <summary>
            ///将Word文档转换成Html文件
            /// </summary>
            /// <param name="docpath"> Word文件所在完整路径</param>
            /// <param name="savepath">生成后Html保存路径</param>
            public static void ConvertWordToHtml(string docpath, string savepath)
            {
               
                try
                {
                    // 1、指定原文件和目标文件
                    object Source = docpath;
                    string SaveHtmlPath = savepath + "\\index.html";
                    object Target = SaveHtmlPath;
                    // 2、缺省参数  //为了保险,只读方式打开
                    object Unknown = Type.Missing;
                    object readOnly = true;
                    object visible = false;
                    // 3、打开doc文件
                    Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
                    Microsoft.Office.Interop.Word.Document doc = newApp.Documents.Open(ref Source, ref Unknown,
                         ref readOnly, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref visible, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                    // 4、指定另存为格式(HTML) 
                    Type docType = doc.GetType();
                    
                    object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;                doc.SaveAs(ref Target, ref format,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown, ref Unknown,
                            ref Unknown, ref Unknown);                // 5、关闭文档和Word程序
                    doc.Close(ref Unknown, ref Unknown, ref Unknown);
                    newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
                    System.IO.StreamReader sr = new System.IO.StreamReader(SaveHtmlPath);
                    string html = sr.ReadToEnd();
                    sr.Close();
                    html = System.Text.RegularExpressions.Regex.Replace(html, @"<body[^>]*>", "<body oncontextmenu='return false' onselectstart='return false'>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(SaveHtmlPath, true, Encoding.GetEncoding("GB2312"));                 sw.Write(html);
                    sw.Close();
                }
                // 处理异常
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.Message);
                }结贴了!