日期:2014-05-18  浏览次数:20862 次

送分了,各位高人们,小弟初学者,想请各位帮个小忙!!!
求代码!!!

怎样把textbox中的数据保存到D:\路径下为txt格式的文本中。
同时注意检查每个txt中信息的最大个数为10000条,如果大于10000,则写入一个新的文件,命名当天日期+N(1=<N)


------解决方案--------------------
textbox是有多行的吧
C# code


string[]strs=textbox.Text.Split ('\n');
int a=(strs.Length%1000==0)?strs.Length:(strs.Length+1);
//存入的a文本个数
string[]str;
for(int i=0;i<a;i++)
{
   //strs分成a个数组str,每个数组存一个文本
   File.WriteAllLines("", str);
}

------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)
{
string aa = "dddddddddddddddddddddddd";
TxtWrite(aa, @"f:\1.txt");
}
//=====================
//描述:写入TXT文件
//返回:void
private void TxtWrite(string strToWrite, string filename)
{
StreamReader sr = new StreamReader(filename, Encoding.Default);
int i = 0;//取得行数
string s;
while ((s = sr.ReadLine()) != null)
{
i++;
}
if (i > 10000)//大于10000行数的时候新建
{
//新建文件夹
string FullFileName = DateTime.Now + ".txt";
FileStream fs1 = new FileStream(FullFileName, FileMode.CreateNew);
fs1.Close();
}
else//否则就是写
{
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.WriteLine(strToWrite);
sw.Close();
}
}
------解决方案--------------------
textbox?
C# code

   string yourfilepath="你要存储的文件路径";
            StreamReader reader = new StreamReader(yourfilepath);
            StreamWriter writer;
            int count = 0;
         //   string[] aa=new string[]{System.Environment.NewLine};
            count = reader.ReadToEnd().Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length;
            //或 while(reader .ReadLine())count++;
            reader.Close();
            if (count > 10000)
            {
                int index = yourfilepath.LastIndexOf("+");
                int n ;
                int.TryParse(index == -1 ? "0" : (yourfilepath.Substring(index == -1 ? 0 : index + 1)),out n);//序号
                int indextemp = yourfilepath.Substring(0, index == -1 ? 0 : index).LastIndexOf("+");
                string temp = yourfilepath.Substring(0, indextemp == -1 ? (yourfilepath.Length - 1) : indextemp);
                string newfilepath = (n == 0 ? string.Empty : temp) + DateTime.Today.ToLongDateString() + "+" + (n + 1).ToString();
                writer = new StreamWriter(newfilepath);
            }
            else writer = new StreamWriter(yourfilepath);
            writer.WriteLine("你要存储的信息");
            writer.Flush(); 
            writer.Close();