现在的位置: 首页 > 综合 > 正文

Asp.NET(asp)写文件的一个小实例程序。

2013年05月24日 ⁄ 综合 ⁄ 共 1713字 ⁄ 字号 评论关闭
 

Asp.NET写文件的一个小实例程序。

  #region 写文件
        /****************************************
         * 函数名称:WriteFile
         * 功能说明:当文件不存时,则创建文件,并追加文件
         * 参    数:Path:文件路径,Strings:文本内容
         * 调用示列:
         *           string Path = Server.MapPath("Default2.aspx");      
         *           string Strings = "这是我写的内容啊";
         *           EC.FileObj.WriteFile(Path,Strings);
        *****************************************/
        ///

        /// 写文件
        ///
        /// 文件路径
        /// 文件内容
        public static void WriteFile(string Path, string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }
        #endregion

 

 

下面是asp的写文件的一个小实例程序。

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<%
cip=Request.ServerVariables("REMOTE_ADDR")
%>
<body>
cip=<%=cip%>
<%
Dim fso, f1, ts, s,strsql,strcityid,strfile
strcityid=request.QueryString("CityID")

strfile="../IPTXT/"&strcityid&".txt"

Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile(server.mappath("../IPTXT/"+strcityid+".txt"), True)
' 写一行。server.mappath("data/bfxx.mdb"
'Response.Write "Writing file <br>"

f1.WriteLine cip

f1.WriteBlankLines(1)
f1.Close
' 读取文件的内容。
'Response.Write "Reading file <br>"
Set ts = fso.OpenTextFile(server.mappath("../IPTXT/"+strcityid+".txt"), ForReading)
s = ts.ReadLine
'Response.Write "File contents = '" & s & "'"
ts.Close

%>

</body>
</html>

抱歉!评论已关闭.