Read strings from files using mask and write them in txt file

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
Hello!
I am interested in
1) how to write strings in txt file There are many conditions in code to do so...
2) And my obstacle also is how to write strings with words like "Vas" or "Tim"

C#:
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Collections.Generic[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Linq[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Text[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Threading.Tasks[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.IO[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Text.RegularExpressions[/COLOR][COLOR=green];[/COLOR]
 
[COLOR=#0600FF]namespace[/COLOR] ConsoleApp1
 
[COLOR=green]{[/COLOR]
    [COLOR=#6666CC]class[/COLOR] Program
 
    [COLOR=green]{[/COLOR]
        [COLOR=#0600FF]static[/COLOR] [COLOR=#6666CC]void[/COLOR] Main[COLOR=green]([/COLOR][COLOR=#6666CC]string[/COLOR][COLOR=green][[/COLOR][COLOR=green]][/COLOR] args[COLOR=green])[/COLOR]
 
        [COLOR=green]{[/COLOR]
 
            DirectoryInfo d [COLOR=green]=[/COLOR] [COLOR=green]new[/COLOR] DirectoryInfo[COLOR=green]([/COLOR][COLOR=#666666]@"C:\Users\Fedor\Desktop\?#folder"[/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
 
            StringBuilder fileList [COLOR=green]=[/COLOR] [COLOR=green]new[/COLOR] StringBuilder[COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
            DateTime from_date [COLOR=green]=[/COLOR] DateTime[COLOR=green].[/COLOR][COLOR=#0000FF]Now[/COLOR][COLOR=green].[/COLOR][COLOR=#0000FF]AddDays[/COLOR][COLOR=green]([/COLOR][COLOR=green]-[/COLOR][COLOR=#FF0000]1[/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
            DateTime to_date [COLOR=green]=[/COLOR] DateTime[COLOR=green].[/COLOR][COLOR=#0000FF]Now[/COLOR][COLOR=green];[/COLOR]
            [COLOR=#0600FF]using[/COLOR] [COLOR=green]([/COLOR]StreamWriter sw [COLOR=green]=[/COLOR] File[COLOR=green].[/COLOR][COLOR=#0000FF]CreateText[/COLOR][COLOR=green]([/COLOR][COLOR=#666666]"list.txt"[/COLOR][COLOR=green])[/COLOR][COLOR=green])[/COLOR]
            
            [COLOR=#0600FF]foreach[/COLOR] [COLOR=green]([/COLOR]FileInfo f [COLOR=#0600FF]in[/COLOR] d[COLOR=green].[/COLOR][COLOR=#0000FF]GetFiles[/COLOR][COLOR=green]([/COLOR][COLOR=#666666]"*.txt"[/COLOR][COLOR=green])[/COLOR][COLOR=green].[/COLOR][COLOR=#0600FF]Where[/COLOR][COLOR=green]([/COLOR]f [COLOR=green]=>[/COLOR] f[COLOR=green].[/COLOR][COLOR=#0000FF]LastWriteTime[/COLOR] [COLOR=green]>=[/COLOR] from_date [COLOR=green]&&[/COLOR] f[COLOR=green].[/COLOR][COLOR=#0000FF]LastWriteTime[/COLOR] [COLOR=green]<=[/COLOR] to_date[COLOR=green])[/COLOR][COLOR=green])[/COLOR]
  
 
                    
            [COLOR=green]{[/COLOR]
                    StreamReader sr [COLOR=green]=[/COLOR] [COLOR=green]new[/COLOR] StreamReader[COLOR=green]([/COLOR]f[COLOR=green].[/COLOR][COLOR=#0000FF]Name[/COLOR], Encoding[COLOR=green].[/COLOR][COLOR=#0600FF]Default[/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
                    
                    [COLOR=#6666CC]string[/COLOR] s [COLOR=green]=[/COLOR] sr[COLOR=green].[/COLOR][COLOR=#0000FF]ReadToEnd[/COLOR][COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
 
                    [COLOR=#6666CC]String[/COLOR] pattern [COLOR=green]=[/COLOR] [COLOR=#666666]@"\b(Vas||Tim)\b"[/COLOR][COLOR=green];[/COLOR]
 
                    [COLOR=#0600FF]if[/COLOR] [COLOR=green]([/COLOR]Regex[COLOR=green].[/COLOR][COLOR=#0000FF]IsMatch[/COLOR][COLOR=green]([/COLOR]s, pattern, RegexOptions[COLOR=green].[/COLOR][COLOR=#0000FF]IgnoreCase[/COLOR][COLOR=green])[/COLOR][COLOR=green])[/COLOR]
 
                    [COLOR=green]{[/COLOR]
                        sw[COLOR=green].[/COLOR][COLOR=#0000FF]WriteLine[/COLOR][COLOR=green]([/COLOR]s[COLOR=green])[/COLOR][COLOR=green];[/COLOR]
                    [COLOR=green]}[/COLOR]
 
                    sr[COLOR=green].[/COLOR][COLOR=#0000FF]Close[/COLOR][COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
 
 
 
                    [COLOR=teal][I]//if (s.Contains("Vasya" || "Ira"))[/I][/COLOR]
 
 
 
                    fileList[COLOR=green].[/COLOR][COLOR=#0000FF]AppendLine[/COLOR][COLOR=green]([/COLOR]f[COLOR=green].[/COLOR][COLOR=#0000FF]Name[/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
 
                [COLOR=teal][I]//sw.WriteLine(f.Name);[/I][/COLOR]
 
            [COLOR=green]}[/COLOR]
 
            Console[COLOR=green].[/COLOR][COLOR=#0000FF]Write[/COLOR][COLOR=green]([/COLOR]fileList[COLOR=green].[/COLOR][COLOR=#0000FF]ToString[/COLOR][COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
            Console[COLOR=green].[/COLOR][COLOR=#0000FF]ReadKey[/COLOR][COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
 
        [COLOR=green]}[/COLOR]
 
    [COLOR=green]}[/COLOR]
 
[COLOR=green]}[/COLOR]
 
Your question is way too vague. There are many specific ways to write text to a text file but they pretty much always come down to using a StreamWriter, either directly or indirectly. You're already using a StreamWriter so...

As for the second part, the answer is that you write them just like any other text.

What you need to do is provide a FULL and CLEAR explanation of the problem, which includes EXACTLY what you're trying to achieve, EXACTLY how you're trying to achieve it and exactly what happens when you try. We shouldn't have to work it out from code that doesn't actually do what you want.
 
Back
Top Bottom