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?
Related
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();
Ok, I'm racking my brains over this one. It's pretty simple though (I think).
I'm currently creating a text file as a comma separated string of values.
Later, I read in that file data and then use the .split function to split the data by commas.
I discovered that sometimes one of the description fields in the data conatins an embedded comma, which ends up throwing the split command off.
Is there any special character I could use that could pretty much guarantee wouldn't be in the data, or is there a better way to accomplish this? Thanks!
// Initial Load
fullString = fileName + "," + String.Join(",", fieldValues);
// Access later
String[] valuesArray = myString.Split(',');
Short answer, there's no "simple" way to do it using Split. The best you can hope for is to set the deliminator as something cooky that wouldn't ever get used (but even that's not a guarantee).
The simple method would be to used something like CsvHelper (get it through Nuget) or any of the other dozen or so packages that are designed for parsing CSV.
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 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
}
I have looked at the split string methods in C#, such as:
string[] lines = Regex.Split(value, "\");
Although I have come across the situation where I need to extract a file name from a file path, so I dont want to have to split the string on all occurrences of "\" e.g.:
C:\Windows\System32\calc.exe
Expected output:
calc.exe
new FileInfo(#"C:\Windows\System32\calc.exe").Name
Let the framework take the strain. Use the Path.GetFilename method.
http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx