逐行读取一个txt文件,从文件夹中读取文件,从文件中随机读取一行,但所有结果都是一样的。这个问题的原因是什么?我想从文件夹中随机抽取一个模板(.html)文件,并从文件中随机抽取一行,但值相同。
if (comboBox1.SelectedItem.ToString() == "Amazon")
{
affpathname = affcase1;
}
else if (comboBox1.SelectedItem.ToString() == "Clickbank")
{
affpathname = affcase2;
}
int postpermm = Convert.ToInt32(postperm.Text);
this.timer1.Start();
string pfile = textBox1.Text;
DateTime startdatef = dateTimePicker1.Value;
string startdateshorta = string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), "{0:yyyy-MM-dd-HH-mm-ss}", DateTime.Now);
FileStream FileOut = new FileStream(Application.StartupPath + @"\" + "wordpress" + @"\" + startdateshorta + ".xml", FileMode.Create);
StreamWriter sw = new StreamWriter(FileOut);
StreamReader a = new StreamReader(pfile);
string dd = null;
while ((dd = a.ReadLine()) != null)
{
{
var rand = new Random();
IEnumerable<string> lines = File.ReadLines(@Application.StartupPath + @"\config\" + affpathname + @"\title_template.txt");
var lineToRead = rand.Next(1, lines.Count());
var line = lines.Skip(lineToRead - 1).First();
sw.Write(line);
sw.Write("\r\n");
}
{
var rr = new Random();
var files = Directory.GetFiles(@Application.StartupPath + @"\config\" + affpathname + @"\layout\", "*.*").Where(s => Regex.Match(s, @"\.(html|htm)$").Success);
string randFile = files.ToList()[rr.Next(0, files.Count())];
sw.Write(randFile);
sw.Write("\r\n");
sw.Write("\r\n");
}
if (comboBox1.SelectedItem.ToString() == "Amazon")
{
affpathname = affcase1;
}
else if (comboBox1.SelectedItem.ToString() == "Other Aff")
{
affpathname = affcase9;
}
string startdatelong = string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), "{0:ddd, dd MMM yyyy HH:mm:ss}", startdatef);
string todaydatelong = string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), "{0:ddd, dd MMM yyyy HH:mm:ss}", DateTime.Now);
string[] arr1 = dd.Split('@');
{
string productname = arr1[0];
string productimg = arr1[1];
string productlink = null;
if (mutiaff.Checked)
{
productlink = arr1[2];
}
else
{
productlink = singleafflink.Text;
}
}
}
sw.Write("</channel>\r\n");
sw.Write("</rss>\r\n");
sw.Close();
FileOut.Close();
}