I am working on a password generator, that uses elements of an array to generate a word-based password.
I currently am working with four arrays, with a lot of elements, and I have to hardcode them individually. I want to automate that process, because writing in a .txt file is both easier and cleaner than writing it on the code itself, and as I plan on distributing this program to my friends, I want to be able to make libraries for the arrays.
Simply put, the .txt file will have four lines, each for one of the arrays.
All I need to know currently is how to import each the lines of the text as a single string, which will be individually formatted into the arrays.
So, for example, the .txt file would have this:
a,b,c,d,e,f,g
d,e,f,g,h,i,j
g,h,i,j,k,l,m
j,k,l,m,n,o,p
And after the "fetching", four different strings would contain each of the lines:
string a = "a,b,c,d,e,f,g"
string b = "d,e,f,g,h,i,j"
string c = "g,h,i,j,k,l,m"
string d = "j,k,l,m,n,o,p"
I will then process it by this, for each string, to break them down into elements.
String pattern = #"\-";
String[] elements = System.Text.RegularExpressions.Regex.Split(passKey, pattern);
You can use this:
System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt");
To put them in an array specifically, use:
string[] lines = File.ReadLines("c:\\file.txt").ToArray();
Related
I have a problem with loading text from file, which appears only on AppVeyor. I'm reading text from file, like:
string input = File.ReadAllText(Path);
Next I want to split this string to array - I want to have each line, I'am doing it like this:
string [] array = input.Split(new string[] { Environment.NewLine });
And on my PC it works. I have some unit test (MSTest) with DeploymentItem attribute and I have lines in that array.
But on AppVeyor, where I want to have CI, that array has only one string.
I've tested it also with VSTestConsole on my PC and it works, so it isn't problem with eg. parameters of VSTest.Console.exe
Is it problem with Environment.NewLine string? I know I could do it by the other way, but:
I want to use String.Split() method because I need StringSplitOptions enum to make my code more readable instead of eg. foreach loop or something else.
I want to know why it isn't working :)
Can you try reading the file using
string[] array = File.ReadLines(Path);
Does that still give you an array with only one item?
This csv file has lots of rows but all the rows doesn't have the equal number of values.
For dealing complex CSV files it is better to use a reliable solution, See this http://joshclose.github.io/CsvHelper/
It's easy to use simple like this
var csv = new CsvReader( textReader );
var records = csv.GetRecords<MyClass>().ToList();
I would use the string split method. https://msdn.microsoft.com/en-us/library/system.string.split(v=vs.110).aspx.
Suppose you read your csv file into a string, you can split the string on line breaks into multiple strings. You'll create a new list for each new line string. You'll split each line on whatever you have for your delimiter, then add those values to your list.
Edit: here is a similar question How to split() a delimited string to a List<String>
I have a question regarding C#, strings and arrays.
I've searched for similar questions at stack overflow, but could not find any answers.
My problem:
I have a string array, which contains words / wordparts to check file names. If all of these strings in the array matches, the word is "good".
String[] StringArray = new String[] { "wordpart1", "wordpart2", ".txt" };
Now I want to check if all these strings are a part of a filename. If this checkresult is true, I want to do something with this file.
How can I do that?
I already tried different approaches, but all doesn't work.
i.e.
e.Name.Contains(StringArray)
etc.
I want to avoid to use a loop (for, foreach) to check all wordparts. Is this possible?
Thanks in advance for any help.
Now I want to check if all these strings are a part of a filename. If this checkresult is true, I want to do something with this file. How can I do that?
Thanks to LINQ and method groups conversions, it can be easily done like this:
bool check = StringArray.All(yourFileName.Contains);
Similar question: Using C# to check if string contains a string in string array
This uses LINQ:
if(stringArray.Any(stringToCheck.Contains))
This checks if stringToCheck contains any one of substrings from
stringArray. If you want to ensure that it contains all the
substrings, change Any to All:
if(stringArray.All(s => stringToCheck.Contains(s)))
I am creating an application which converts a MS Access table and an Excel sheet to .csv files and then differences the access table with the excel sheet. The .csv files are fine but the resulting difference file has errors in fields that contain html (the access table has fields with the html). I'm not sure if this is a special character issue because the special characters were not an issue in creating the .csv file in the first place, or if it is an issue with the way I am differencing the two files.
Part of the problem I suppose could be that in the access .csv file, the fields that contain the html are formatted so that some of the information is on separate lines instead of all on one line, which could be throwing off the reader, but I don't know how to correct this issue.
This is the code for creating the difference file:
string destination = Form2.destination;
string path = Path.Combine(destination, "en-US-diff.csv");
string difFile = path;
if (File.Exists(difFile))
{
File.Delete(difFile);
}
using (var wtr = new StreamWriter(difFile))
{
// Create the IEnumerable data sources
string[] access = System.IO.File.ReadAllLines(csvOutputFile);
string[] excel = System.IO.File.ReadAllLines(csvOutputFile2);
// Create the query
IEnumerable<string> differenceQuery = access.Except(excel);
// Execute the query
foreach (string s in differenceQuery)
{
wtr.WriteLine(s);
}
}
Physical line versus logical line. One solution is to use a sentinel, which is simply an arbitrary string token selected in such a way so as not to confound the parsing process, for example "##||##".
When the input files are created, add the sentinel to the end of each line...
1,1,1,1,1,1,###||##
Going back to your code, the System.IO.File.ReadAllLines(csvOutputFile); uses the Environment.Newline string as its sentinel. This means that you need to replace this statement with the following (pseudo code)...
const string sentinel = "##||##";
string myString = File.ReadAllText("myFileName.csv");
string[] access = myString.Split(new string[]{sentinel},
StringSplitOptions.RemoveEmptyEntries);
At that point you will have the CSV lines in your 'access' array the way you wanted as a collection of 'logical' lines.
To make things further conformant, you would also need to execute this statement on each line of your array...
line = line.Replace(Environment.NewLine, String.Empty).Trim();
That will remove the culprits and allow you to parse the CSV using the methods you have already developed. Of course this statement could be combined with the IO statements in a LINQ expression if desired.
I want to send multi attach in email, but have problem with those. When I put all files what want to send in one string always get error, but when put one file in one attach inside of loop thing work.
Now I have problem with copying one part of string in to another strings, don't know how to do that, do you have some solution?
Example:
txtattach.Text = "d:\\folder\\file1,d:\\folder\\file2,d:\\folder\\file3";
want to get 3 strings with context of location without "," that I can easy put it in loop.
use the split function:
string[] paths = txtattach.Text.Split(',');
One way to do this is using the Split method so you easily can iterate over the items in a loop:
foreach(var filename in txtAttach.Text.Split(','))
{
// Do something with filename
}