Hey i want to generate Random Texts from 4 different Textfiles. The first File have 2 Textslines, the secound have 2 Textline the thrid have 3 textlines and the fourth have 1 textline. With that it should be possible to generate 2x2x3x1 different texts.
I dont know why but the programm altimes just generate 2 different texts and then it runs in a endless loop.
Any idea what i can do to avoid this? - Normaly it should end with 4 different texts.
If i add to the other textfiles with just 2 lines 1 additional line, so that all files have 3 lines and just the last textfile have 1 line it works.
But normaly it should work with 2 lines too.
the Lists count 2 Lines and randam should generate random number between 0 and 2 minus 1 so its alltimes random between 0 and 1 and should select a random index in the list 0 or 1. But with 2 elements in the list it alltimes select the same index.
I use the following code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace VKTextGenerator
{
class Program
{
string sFullText = "";
string sFirstTextPart = "";
string sSecoundTextPart = "";
string sThirdTextPart = "";
string sLinkText = "";
int iAnzahlTexte = 4;
static void Main(string[] args)
{
Random rnd = new Random();
Program self = new Program();
List<string> allTexts = new List<string>();
self.deleteFullTextFile();
for (int i = 0; i < self.iAnzahlTexte; i++)
{
self.ReadFirstTextPart();
Thread.Sleep(rnd.Next(10, 5000));
self.ReadSecoundTextPart();
Thread.Sleep(rnd.Next(10, 5000));
self.ReadThirdTextPart();
self.ReadLinkText();
self.sFullText = self.sFirstTextPart + " " + self.sSecoundTextPart + " " + self.sThirdTextPart + " " + self.sLinkText;
if (!allTexts.Contains(self.sFullText))
{
allTexts.Add(self.sFullText);
self.AppendTextToFile();
}
else
{
i--;
}
}
}
internal static string GetStringSha256Hash(string text)
{
if (String.IsNullOrEmpty(text))
return String.Empty;
using (var sha = new System.Security.Cryptography.SHA256Managed())
{
byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
byte[] hash = sha.ComputeHash(textData);
return BitConverter.ToString(hash).Replace("-", String.Empty);
}
}
public void deleteFullTextFile()
{
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = removeExeFromPathChar(path);
path = path + @"\FinishedText\";
VerifyDir(path);
string fileName = "FullTexts.txt";
if (File.Exists(path + fileName))
{
File.Delete(path + fileName);
}
}
public void ReadFirstTextPart()
{
List<string> textList = new List<string>();
Random rnd = new Random();
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = removeExeFromPathChar(path);
path = path + @"\Config\";
string fileName = "FirstTextPart.txt";
textList = ReadTextFile(path + fileName);
if (textList.Count > 0)
{
sFirstTextPart = textList[rnd.Next(0, textList.Count - 1)];
}
}
public void ReadSecoundTextPart()
{
List<string> textList = new List<string>();
Random rnd = new Random();
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = removeExeFromPathChar(path);
path = path + @"\Config\";
string fileName = "SecoundTextPart.txt";
textList = ReadTextFile(path + fileName);
if (textList.Count > 0)
{
sSecoundTextPart = textList[rnd.Next(0, textList.Count - 1)];
}
}
public void ReadThirdTextPart()
{
List<string> textList = new List<string>();
Random rnd = new Random();
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = removeExeFromPathChar(path);
path = path + @"\Config\";
string fileName = "ThirdTextPart.txt";
textList = ReadTextFile(path + fileName);
if (textList.Count > 0)
{
sThirdTextPart = textList[rnd.Next(0, textList.Count - 1)];
}
}
public void ReadLinkText()
{
List<string> textList = new List<string>();
Random rnd = new Random();
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = removeExeFromPathChar(path);
path = path + @"\Config\";
string fileName = "LinkText.txt";
textList = ReadTextFile(path + fileName);
if (textList.Count > 0)
{
sLinkText = textList[rnd.Next(0, textList.Count - 1)];
}
}
private static List<String> ReadTextFile(string path)
{
List<String> lines = new List<String>();
using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
{
while (sr.Peek() != -1)
lines.Add(sr.ReadLine());
}
return lines;
}
public void AppendTextToFile()
{
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = removeExeFromPathChar(path);
path = path + @"\FinishedText\";
VerifyDir(path);
string fileName = "FullTexts.txt";
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(path + fileName, true);
file.WriteLine(sFullText);
file.Close();
}
catch (Exception) { }
}
public static String removeExeFromPathChar(String s)
{
int exeLength = "VKTextGenerator.exe".Length;
return (s == null || s.Length == 0)
? null
: (s.Substring(0, s.Length - exeLength));
}
public void VerifyDir(string path)
{
try
{
DirectoryInfo dir = new DirectoryInfo(path);
if (!dir.Exists)
{
dir.Create();
}
}
catch { }
}
}
}
Aucun commentaire:
Enregistrer un commentaire