怎么创建一个文本文件,文件名自己命名,同时向里面写数据.

解决方案 »

  1.   

    http://battler.cnblogs.com/archive/2005/05/24/161293.html
      

  2.   


    protected void Write_Txt(string FileName)
        {
            Encoding code = Encoding.GetEncoding("gb2312");
            string htmlfilename = HttpContext.Current.Server.MapPath("/txt/" + FileName + ".txt");
            string str = "";
            StreamWriter sw = null;
            {
                try
                {
                    sw = new StreamWriter(htmlfilename, false, code);
                    sw.Write(str);
                    sw.Flush();
                }
                catch { }
            }
            sw.Close();
            sw.Dispose();
        }
      

  3.   

    http://www.cftea.com/c/2007/06/OWNMPDV89NDYMAIM.asp
      

  4.   

    using(StreamWriter sw =  new StreamWriter(path))
    {
                    sw.Write(str);
                    sw.Flush();
     }
      

  5.   

    FileStream objFileStream;
    StreamWriter objStreamWriter;
    objFileStream = new FileStream(“D:\\test.txt”, FileMode.OpenOrCreate, FileAccess.Write);
    objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
    objStreamWriter.WriteLine("nihao");
    objStreamWriter.Close();
    objFileStream.Close();试试看
      

  6.   

    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            // Create an instance of StreamWriter to write text to a file.
            // The using statement also closes the StreamWriter.
            using (StreamWriter sw = new StreamWriter("TestFile.txt")) 
            {
                // Add some text to the file.
                sw.Write("This is the ");
                sw.WriteLine("header for the file.");
                sw.WriteLine("-------------------");
                // Arbitrary objects can also be written to the file.
                sw.Write("The date is: ");
                sw.WriteLine(DateTime.Now);
            }
        }
    }
      

  7.   


    public void WriteLogFile(String sContent,String sSvrPath)
    {
    String sLogPath;
    String sFileName;
    String sTime;
    StreamWriter SW;
    sFileName = DateTime.Now.Date.ToString("yyyyMMdd") + ".txt";
    sLogPath = sSvrPath + @"file\" + sFileName;
    SW = File.CreateText(sLogPath);
    SW = File.AppendText(sLogPath);

    sTime = DateTime.Now.TimeOfDay.ToString();
    SW.Write("["+sTime+"]  ");
    SW.WriteLine(sContent);
    SW.Flush();
    SW.Close(); }
      

  8.   

    基本的文件操作都不知道吗?看看MSDN吧
      

  9.   

    <?
    //基于文件的计数器
    if(!file_exists("count.txt"))//如计数文件不存在,创建之
    {
        exec("echo 0>count.txt");
    }$fp=fopen("count.txt","r+"); //以可读写方式打开计数文件
    $fs=filesize("count.txt");//得到文件大小
    $cont=fgets($fp,$fs+1);//根据文件大小取出数据$cont+=1;//记录数加一
    fseek($fp,0);//移动文件指针
    fputs($fp,$cont);//将新纪录数写到文件指针
    fclose($fp);//关闭计数文件echo"该页面已被访问 $cont 次!";//输出计数
    ?>
      

  10.   

    Encoding GB2312= Encoding.GetEncoding("gb2312");//设置一下编码格式
    StreamWriter  sw = new StreamWriter(要写入的文件路径, false, GB2312);
    sw.Write(str);
    sw.Flush();
    sw.Close();
    sw.Dispose();
      

  11.   

    System.IO.File.Create(path);
    System.IO.File.WriteAllText(path, content);
      

  12.   

    引入命名空间using System.Text;
    using System.IO;
    代码如下  放置一个按钮和两个文本框
    protected void Button1_Click(object sender, EventArgs e)
        {
            string name = this.txtname.Text.Trim().ToString();
            string content = this.txtcontent.Text.Trim().ToString();
            Write_Txt(name,content);
        }    protected void Write_Txt(string FileName,string content)
        {
            Encoding code = Encoding.GetEncoding("gb2312");
            string htmlfilename = HttpContext.Current.Server.MapPath("~/" + FileName + ".txt");
            string str = "";
            StreamWriter sw = null;
            {
                try
                {
                    sw = new StreamWriter(htmlfilename, false, code);
                    sw.Write(content);
                    sw.Flush();
                }
                catch { }
            }
            sw.Close();
            sw.Dispose();
        }
      

  13.   

    static public class Logs
        {
            static object o = new object();
            readonly static string _logpath = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
            public enum LogTypeEnum
            {
                SystemLog,
                UserLoginLog,
                OtherLog
            }
            /// <summary>
            /// 写Log文件
            /// </summary>
            /// <param name="lt">类型</param>
            /// <param name="LogsContent">内容</param>
            public static void WriterLogs(LogTypeEnum lt, string LogsContent)
            {
                lock (o)
                {
                    string sql = "INSERT INTO SysLogs(LogType,Content) VALUES(@LogType,@Content)";
                    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql))
                    {
                        cmd.Parameters.Add(DataAccess.CreateSqlParameterVarChar("@LogType", Enum.GetName(typeof(LogTypeEnum), lt), 50));
                        cmd.Parameters.Add(DataAccess.CreateSqlParameterNText("@Content", LogsContent));
                        DataAccess.ExcuteCmd(cmd);
                    }
                    WriterLogsByTxt(lt, LogsContent);
                    //WriterXml(lt, LogsContent);
                }
            }
            static public void WriterLogs(LogTypeEnum lt, Exception ex)
            {
                WriterLogs(lt, ex.Message);
            }
            static void WriterLogsByTxt(LogTypeEnum lt, string LogsContent)
            {
                DirectoryInfo dir = new DirectoryInfo(_logpath);
                try
                {
                    if (!dir.Exists)
                        dir.Create();
                    string filename = "NewJxc_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".log";
                    FileInfo file = new FileInfo(dir + filename);
                    if (!file.Exists)
                        file.Create();                using (FileStream fs = file.OpenWrite())
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.BaseStream.Seek(0, SeekOrigin.End);
                            sw.Write("{0}[{1}]:{2}", DateTime.Now.ToLongTimeString(), Enum.GetName(typeof(LogTypeEnum), lt), LogsContent);
                            sw.Write("\n");
                            sw.Flush();
                        }
                    }
                }
                catch 
                {
                    ;
                }
            }
            static string FileName
            {
                get
                {
                    DirectoryInfo dir = new DirectoryInfo(_logpath);
                    if (!dir.Exists)
                        dir.Create();
                    string filename = "log_NewJxc_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".xml";
                    filename = dir + filename;
                    FileInfo f = new FileInfo(filename);
                    if (!f.Exists)
                        f.Create();
                    return filename;
                }
            }
            public static string ReadLogFile(string filename)
            {
                string fullname = _logpath + filename;
                FileStream fs = new FileStream(fullname, FileMode.OpenOrCreate, FileAccess.Read);
                System.Text.StringBuilder output = new System.Text.StringBuilder();
                output.Length = 0;
                StreamReader read = new StreamReader(fs);
                read.BaseStream.Seek(0, SeekOrigin.Begin);
                while (read.Peek() > -1)
                    output.Append(read.ReadLine() + "\n");
                read.Close();            return output.ToString();
            }
            public static FileInfo[] GetLogFiles()
            {
                DirectoryInfo dir = new DirectoryInfo(_logpath);
                return dir.GetFiles("*.log");
            }
        }
      

  14.   


    using System.IO;                //检查目录是否存在,如果不存在则创建。                if (!Directory.Exists(fileDir))
                    {
                        Directory.CreateDirectory(fileDir);
                    }                //检查文件是否存在                if (File.Exists(file))
                    {
                        //执行需要的操作
                    }