【发布时间】:2015-01-22 21:57:21
【问题描述】:
我创建了一个创建 1 个文本文件的网络应用程序。在这个文本文件中,它创建了 1000 行,其中包含相同的单词“TRY AGAIN”。在这之后每 50 行我放一个随机代码,这意味着在 1000 行中,20 行是随机的。 这是我的代码:
static Random randNum = new Random();
public static string Random(int ran)
{
string _charachters = "ABCDEFGHIJKMLNOPQRSTUVWXYZ0123456789";
char[] chars = new char[ran];
int allowedCharCount = _charachters.Length;
for (int i = 0; i < ran; i++)
{
chars[i] = _charachters[(int)((_charachters.Length) * randNum.NextDouble())];
}
return new string(chars);
}
protected void Button1_Click(object sender, EventArgs e)
{
string pathCreate = @"C:\" + TextBox3.Text + ".txt";
if (!File.Exists(pathCreate))
{
using (StreamWriter sw = File.CreateText(pathCreate))
{
for (int i = 1; i <= int.Parse(TextBox1.Text); i++)
{
sw.WriteLine("TRY AGAIN.");
}
}
}
string pathRandom = @"C:\" + TextBox3.Text + ".txt";
string[] lines = File.ReadAllLines(pathRandom);
for (int i = 0; i < lines.Length; i += int.Parse(TextBox2.Text))
{
lines[i] = lines[i].Replace("TRY AGAIN.", Random(int.Parse("7")));
}
File.WriteAllLines(pathRandom, lines);
}
现在我想通过单击按钮创建 2 个或更多文本文件。并且在每个文本文件上都会有随机代码(不重复)。任何想法? 谢谢。
【问题讨论】:
-
无需冒犯。评论标记为审核。
标签: asp.net asp.net-mvc visual-studio-2010 visual-studio razor