When I call the Generate function it doesn't Create the StreamWriter object and instead throws an exception that says :
file used by another process
but the file isn't open and this is the first Stream that is using it.
public static string GetWindowsUserName()
{
string st = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
bool Condition = false;
foreach (char ch in st)
{
if (ch == '\\')
Condition = true;
}
if (Condition)
{
string[] stArr = st.Split('\\');
st = stArr[stArr.Length - 1];
}
return st;
}
public static void Generate(bool Desktop, bool RemoveLast, bool InExistingTxt, int Count, int Length)
{
Random Generator = new Random();
if (Desktop)
path = $"C:\\Users\\{GetWindowsUserName()}\\Desktop\\GeneratedNumbers.txt";
else
path = "GeneratedNumbers.txt";
if (!InExistingTxt && !RemoveLast)
File.Create(path);
else if (!InExistingTxt && RemoveLast)
{
if (File.Exists(path))
{
File.Delete(path);
}
File.Create(path);
}
System.Threading.Thread.Sleep(1000);
if (File.Exists(path))
{
StreamWriter SW = new StreamWriter(path);
for (int i = 0; i < Count; i++)
{
string st = "";
for (int j = 0; j < Length; j++)
{
int o = Generator.Next(0, 11);
st += Convert.ToString(o);
}
SW.WriteLine(st);
}
SW.Dispose();
}
}
File.Create returns a stream to the created file. Since you're not disposing the stream, you have an error when trying to re-open the same file.
I also suspect that you messed up your "RemoveLast" logic. I'll assume that you want to append content to the existing file when it's set to false:
if (InExistingTxt && !File.Exists(path))
return;
StreamWriter SW;
if (RemoveLast)
SW = File.CreateText(path);
else
SW = File.AppendText(path);
using (SW)
{
for (int i = 0; i < Count; i++)
{
string st = "";
for (int j = 0; j < Length; j++)
{
int o = Generator.Next(0, 11);
st += Convert.ToString(o);
}
SW.WriteLine(st);
}
}
Related
So I am creating a hangman-type game in C# and I want the user's input to be recognized as a variable. So I am trying to use this variable(user input) and see if it is found in the word chosen. However, there is no .Contain function that works with "char" variables. Is there any way to make this work? (The part in asterisks is the portion I want to add this code to)
using System;
namespace Hangman
{
class WordChoice
{
static void Main(string[] args)
{
Random rand = new Random();
int numword = rand.Next(2);
string word = "";
char[] ltrlist = { ' ' };
char ltrchce = ' ';
int strlength = 0;
**void ltrcheck()
{
if
{
Console.WriteLine("Great you got a letter");
}
}**
if (numword == 1)
{
word = "Shrek";
strlength = word.Length;
ltrlist = new char[strlength];
for (int a = 0; a < strlength; a++)
{
ltrlist[a] = word[a];
}
}
if (numword == 2)
{
word = "Venom";
strlength = word.Length;
ltrlist = new char[strlength];
for (int a = 0; a < strlength; a++)
{
ltrlist[a] = word[a];
}
}
if (numword == 3)
{
word = "Avengers";
strlength = word.Length;
ltrlist = new char[strlength];
for (int a = 0; a < strlength; a++)
{
ltrlist[a] = word[a];
}
}
if (numword == 4)
{
word = "Inception";
strlength = word.Length;
ltrlist = new char[strlength];
for (int a = 0; a < strlength; a++)
{
ltrlist[a] = word[a];
}
}
if (numword == 5)
{
word = "Batman";
strlength = word.Length;
ltrlist = new char[strlength];
for (int a = 0; a < strlength; a++)
{
ltrlist[a] = word[a];
}
}
string undscr = "";
for (int i = 0; i < strlength; i++)
{
undscr = undscr + " _";
}
Console.WriteLine(undscr);
Console.WriteLine("");
Console.WriteLine("Pick a letter");
string ltrchcestr = Console.ReadLine();
ltrchce = Convert.ToChar(ltrchcestr);
ltrcheck();
}
}
}
You can do a function like this
static bool Contains(IEnumerable < char > items, char letter)
=> return items?.Any(c => c == letter) == true;
or just create a typeExtension, for Project wide use:
public static class CharExtensions {
public static bool Contains(this IEnumerable<char> items, char letter ){
return items?.Any(c => c == letter) == true;
}
}
this test will now work:
var chars = new []{'a','b','c'};
Console.WriteLine(chars.Contains('m'));
Console.WriteLine(chars.Contains('b'));
Hi I'm trying to create a code generator that automatically saves the code in a file but when I do it in an infinite loop it doesn't work I've already tried with lock(obj) but it doesn't work.
I have the error "This file is already being used by another process" in an infinite loop
class Generator
{
public static int maxDigit;
public static int codeNumber;
public static void start()
{
for (int i = 0; i < codeNumber; i++)
{
Generate(16, maxDigit);
}
}
public static void Generate(int length, int maxDigit)
{
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var stringChars = new char[length];
var random = new Random();
int counDigit = 0;
int i = 0;
while (i < stringChars.Length)
{
char c = chars[random.Next(chars.Length)];
if ("0123456789".Contains(c) && counDigit < maxDigit)
{
counDigit++;
stringChars[i] = c;
i++;
}
else if (!("0123456789".Contains(c)))
{
stringChars[i] = c;
i++;
}
}
var finalString = new String(stringChars);
string filePath = #"Codes.txt";
using (StreamWriter sw = new StreamWriter(filePath, true))
{
sw.WriteLine(finalString);
}
}
}
You are opening the file on the start() then you are trying to open it again on the Generate method.
Try this:
public static void start()
{
for (int i = 0; i < codeNumber; i++)
{
//using (StreamWriter sw = File.AppendText("Codes.txt"))
{
Generate(16, maxDigit);
}
}
}
then call it using:
Generator.maxDigit = 9;
Generator.codeNumber = 4;
Generator.start();
then it generates Codes.txt
e0uhHvWadGpsHooy
njxQZ6NxN0fiEOYE
QTlZfyTGfHOfWAFJ
d2OXXzhJ1R39Z32d
I know this has been asked multiple times before but I still can't find what's the cause of this. I'm sure the file isn't being used by an external program, so the cause must be in this method. The error occurs in the line where I initialize StreamWriter object for the second time (int the for loop).
static void WriteData(PeopleContainer people)
{
var file = new StreamWriter(File.Open(Directory + Output1, FileMode.Create), Encoding.GetEncoding(1257));
for (int i = 0; i < people.SeenByEveryone.Count; i++)
{
Film film = people.SeenByEveryone.GetFilm(i);
file.WriteLine("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}{0}{8}", CsvSeparator, film.Title, film.Year,
film.Director, film.Genre, film.Company, film.BoxOffice, film.Actors[0], film.Actors[1]);
}
for (int i = 0; i < people.Count; i++)
{
Person person = people.GetPerson(i);
string path = String.Format("{0}Rekomendcija_{1}_{2}.csv", Directory, person.FirstName, person.LastName);
file = new StreamWriter(File.Open(path, FileMode.Create), Encoding.GetEncoding(1257));
for (int j = 0; j < person.RecomemdedFilms.Count; j++)
{
Film film = person.RecomemdedFilms.GetFilm(j);
file.WriteLine("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}{0}{8}", CsvSeparator, film.Title, film.Year,
film.Director, film.Genre, film.Company, film.BoxOffice, film.Actors[0], film.Actors[1]);
}
}
}
You should dispose your Stream.Writer when you are finished with it.
using(var file = new StreamWriter(File.Open(Directory + Output1, FileMode.Create), Encoding.GetEncoding(1257)))
{
//your loop
}
for (int i = 0; i < people.Count; i++)
{
using(file = new StreamWriter(File.Open(path, FileMode.Create), Encoding.GetEncoding(1257)))
{
// other loop
}
}
EDIT:
Like #enkryptor mention: if you want all the recommended films to be in your file in the second loop, you should use StreamWriter not in your loop. Also you should put FileMode.OpenOrCreate
using(file = new StreamWriter(File.Open(path, FileMode.OpenOrCreate), Encoding.GetEncoding(1257)))
{
for (int i = 0; i < people.Count; i++)
{
file.WriteLine(...//your stuff)
}
}
I really need a solution for the next scenario(I've been searching for hours and beating about the bushes to find a smooth solution, but none worked):
I have a winform that:
parse a text file
generate some folders using random words from that file
My code so far:
int value;
string path = null;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
path = fbd.SelectedPath;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (path != null && Directory.Exists(path))
for (int i = 0; i < value; i++)
{
Random rnd = new Random();
string tick1 = rnd.Next(0, 10).ToString();
var lines = File.ReadAllLines(#"M:\\dictionar.txt");
var r = new Random(DateTime.Now.Millisecond);
var randomLineNumber = r.Next(0, lines.Length - 1);
var line = lines[randomLineNumber];
StringBuilder b = new StringBuilder();
for (int j = 0; j < line.Length; j++)
{
char c = line[j];
if (rnd.Next(2) == 0)
{
c = Char.ToUpper(c);
}
b.Append(c);
if (j % 3 == 2)
{
b.Append(rnd.Next(10));
}
}
line = b.ToString();
Directory.CreateDirectory(Path.Combine(path, string.Format("{0}", line.Insert(2, tick1).Insert(4, tick1).Insert(6, tick1))));
}
}
Is there a way to use ToUpper() method as line.Insert() one so that I can get random upper letters? More, is there a better way of randomizing those index positions form line.Insert() (I'm asking this because when it's generating the folders name: the indexes are the same: e.g pe8rs8on8al and just after that the index changes.)?
I want to achieve the following:
if I have the next words in the .txt file:
personal
football
programming
computer
I would like the folder names to look like:
Pe3rs9oN1al
fO8ot5Ba6lL
You can loop through the characters in the string and build a new string depending on random values:
StringBuilder b = new StringBuilder();
for (int i = 0; i < line.Length; i++ ) {
char c = line[i];
if (rnd.Next(2) == 0) {
c = Char.ToUpper(c);
}
b.Append(c);
if (i % 2 == 1) {
b.Append(rnd.Next(10));
}
}
line = b.ToString();
Note: You shouldn't create Random objects in the loop. You should create a single Random object before the loop and use for all random numbers that you need. Creating instances too close in time will make them return the same sequences of random numbers. Also, you don't need to seed the random generator from the clock, the constructor without parameters does that automatically:
Random rnd = new Random();
So, the code in the method would be:
if (path != null && Directory.Exists(path))
Random rnd = new Random();
for (int i = 0; i < value; i++)
{
var lines = File.ReadAllLines(#"M:\\dictionar.txt");
var randomLineNumber = rnd.Next(0, lines.Length);
var line = lines[randomLineNumber];
StringBuilder b = new StringBuilder();
for (int j = 0; j < line.Length; j++)
{
char c = line[j];
if (rnd.Next(2) == 0)
{
c = Char.ToUpper(c);
}
b.Append(c);
if (j % 2 == 1)
{
b.Append(rnd.Next(10));
}
}
line = b.ToString();
Directory.CreateDirectory(Path.Combine(path, line));
}
}
Note the rnd.Next(0, lines.Length) to pick a random line. The upper limit for the random number is not inclusive, so if you use rnd.Next(0, lines.Length - 1) it will never pick the last line.
That's because you are specifying only tick1 in the same loop. If you want to change this, add additional ticks to your code as below:
string tick1 = rnd.Next(0, 10).ToString();
string tick2 = rnd.Next(0, 10).ToString();
string tick3 = rnd.Next(0, 10).ToString();
Then use those in your formatting of the string:
Directory.CreateDirectory(Path.Combine(path, string.Format("{0}", line.Insert(2, tick1).Insert(4, tick2).Insert(6, tick3))))
Like Guffa said you should not use Random in a loop, in all preference you should only instanciate one of it, but I think you could use it like this
public static class StringRandomize
{
static readonly Random rnd = new Random();
static char[] permmitedCharacters { get; set; }
static StringRandomize()
{
List<char> Chars= new List<char>();
for (int i = 48; i < 48+10; i++)
{
Chars.Add((char)i);
}
for (int i = 65; i < 65+26; i++)
{
Chars.Add((char)i);
}
permmitedCharacters = Chars.ToArray();
}
public static string Randomize(string input, double RandomizePercent = 30)
{
StringBuilder result = new StringBuilder();
int index = 0;
while (index < input.Length)
{
if (rnd.Next(0, 100) <= RandomizePercent)
{
if (rnd.Next(0, 100) <= RandomizePercent)
{
result.Append(GenerateCaracter());
}
else
{
if (rnd.Next(0, 100) > 50)
{
result.Append(input.ToLower()[index]);
}
else
{
result.Append(input.ToUpper()[index]);
}
index++;
}
}
else
{
result.Append(input[index]);
index++;
}
}
return result.ToString();
}
private static char GenerateCaracter()
{
return permmitedCharacters[rnd.Next(0, permmitedCharacters.Length)];
}
}
private static void GenerateRandomDirectories(string path, int value)
{
//I'm supposing value is the number of lines that you want
var lines = File.ReadAllLines(#"M:\\dictionar.txt");
Random rnd = new Random();
if (path != null && Directory.Exists(path))
{
for (int i = 0; i < value; i++)
{
Directory.CreateDirectory(path + "\\" + StringRandomize.Randomize(lines[rnd.Next(0,lines.Length)]));
}
}
}
"pers3o7Nal"
"foOtBaLl"
Got like this
public Form1()
{
InitializeComponent();
string content = "";
using (FileStream fs = new FileStream("D:\\names.txt", FileMode.Open, FileAccess.Read))
using (StreamReader sr = new StreamReader(fs))
content = sr.ReadToEnd();
string[] names = content.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
string path = "D:\\RandDirs";
if (!Directory.Exists(path))Directory.CreateDirectory(path) ;
for (int i = 0; i < 50; i++) Directory.CreateDirectory(path + "\\" + getRandomName(names));
}
Random randName = new Random();
Random insertingNumber = new Random();
Random randUpper = new Random();
Random randInsertNumber = new Random();
string getRandomName(string[] names)
{
string name = names[randName.Next(names.Length)];
name = name.Replace(" ", "");
string result = "";
for (int i = 0; i < name.Length; i++)
result += (randUpper.Next(0, 9) <= 5 ? name[i].ToString().ToLower() : name[i].ToString().ToUpper())
+ (((i + 1) % 2 == 0) ? insertingNumber.Next(0, 9).ToString() : "");
return result;
}
as per your needs, i've changed from randomly inserting numbers to inserting number every 2 characters.
I would like to handle the FileNotFoundException within this class if an incorrect file name is keyed in but cannot seem to keep everything in scope using try catch blocks. Any pointer please
The exception is thrown here (FileStream fileStream = File.OpenRead(fileName))
public class XcelReader
{
private readonly List<List<IRule>> m_Rules;
public XcelReader(List<List<IRule>> rules)
{
m_Rules = rules;
}
public void ValidateWorksheet(string fileName)
{
bool allRulesPassed = true;
WorkbookProvider workbookProvider = new WorkbookProvider();
int counter = 0;
IWorkbook workbook;
using (FileStream fileStream = File.OpenRead(fileName))
workbook = workbookProvider.GetWorkbook(fileStream, SpreadsheetType.Xlsx);
for (int rowCounter = 1; rowCounter < workbook.Worksheets[1].Rows.Count; rowCounter++)
{
IRow row = workbook.Worksheets[1].Rows[rowCounter];
for (int columnCounter = +1; columnCounter < row.Cells.Count; columnCounter++)
{
List<string> failedRules = ColumnValueIsValid(row.Cells[columnCounter].Value, m_Rules[columnCounter]);
failedRules.ForEach(failedRule =>
{
allRulesPassed = false;
Console.WriteLine("\n[{0}:{1}] Failed: {2}", rowCounter +1, GetColumnHeaderValue(columnCounter + 1), failedRule);
counter++;
if (counter > 20)
{
Console.WriteLine("\nHit enter to page down................");
Console.ReadLine();
counter = 0;
}
});
}
}
if(allRulesPassed)
Console.WriteLine(#"WOOHOO! worksheet is hunky dory Hit enter to continue");
}
private string GetColumnHeaderValue(int columnCounter)
{
return new Base26Converter().FromBase10(columnCounter);
}
private List<string> ColumnValueIsValid(string value, List<IRule> rules)
{
List<string> failedRules = new List<string>();
rules.ForEach(rule =>
{
if(!rule.IsValid(value))
failedRules.Add(rule.GetReasonForFailure(value));
});
return failedRules;
}
}