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
}
Related
Ok i might sound really stupid here but. I have a list, lets call it example
List<string> example = new List<string>();
example.Add("abc123");
//...
and everytime the user runs the program it detects an input
string input = Console.ReadLine();
and i want it so that if it detects something new that isnt in the list it will add it to the list.
or is there a workaround like putting the words in a different txt file and it can just write it onto said file and putting it on the list.
Thanks in advance.
For those of you who has encountered the same problem as mine. You could just put the items you want to put in your list to a different txt file. and then
string example = Console.ReadLine();
string savedExample = Environment.NewLine + example;
List<string> exampleList = File.ReadAllLines("listOfItems.txt").ToList();
and to add your item into the list
File.AppendAllText(#"listOfItems.txt", savedExample);
It will add example into the list which is the users input.
The Environment.NewLine is to add a new line so the program wont just keep writing on the same line
Note: the listOfItems.txt is expected to be in the same file path as your program.cs or simply you can replace it with a path
(edit because i forgot to mention): make sure your listOfItems.txt is something like
hello
abc123
test
example
//and so on
and not something like
exampleList.Add("hello");
exampleList.Add("abc123");
//...
I am trying to write to a file with around 20 fields. That part works. The first field needs to be unique (can't have duplicates). I cannot figure out how to take only the first field(column) and compare it to the rest in that column. Also I am not sure how to get the variable from the file. The field is a combination of the first and last name. That works just fine but I need to run through the userName and compare it to the rest of them. If another one is the exact same, I need a number added to one of them.
This is what I started but I am not sure now to continue:
class userName
{
public string Name(string userName)
{
string initialUserName
string tempUserName = string.Empty;
}
}
This is how I create the user name:
username = firstName.Substring(0, 4) + lastName.Substring(0, 4);
Without knowing the specifics of how you are writing these "fields," im assuming a single line (row) of your file contains all 20 fields. Hopefully you have delimited each of these fields. You should be able to get all the variables in the first column by reading the lines of the file, one at a time, and using the String.Split method to get a string array containing all of your 20 fields. Simply compare the first array member to your new value, if its equal then append a number to the new string, continue this until all rows are parsed.
Hope this will help. Use streamreader and streamwriter to read and write the text file. Create hashtable store the column read value in it. If you find same content just don't write it.
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?
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.