Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i have a file .ini and i want to check if some words existe in this file .ini. for exxample if in my file .ini there is this string "2605=kl ....."
how do i to check if the "2605" existe in my file ??
i want to do it with c#

i tried this code but it doesn't work
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            StreamReader sr;
string[]lines={""};
 
string[] logFiles = Directory.GetFiles("C:\\r");
 
foreach (string file in logFiles)
{
 
    using (sr = new StreamReader(file))
    {
        foreach (string line in lines)
        {
            if (line.Contains("2036") )
                {
                System.Console.WriteLine("trouvé");
            }
        }
        
    }
    }
 
 
        }
 
     
        
    }
}


please help

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            StreamReader sr;
string[]lines={""};
 
string[] logFiles = Directory.GetFiles("C:\\r");
 
foreach (string file in logFiles)
{
 
    using (sr = new StreamReader(file))
    {
        foreach (string line in lines)
        {
            if (line.Contains("2036") )
                {
                System.Console.WriteLine("trouvé");
            }
        }
        
    }
    }
 
 
        }
 
     
        
    }
}
Posted
Updated 27-May-16 0:34am
v2
Comments
George Jonsson 26-May-16 18:57pm    
A bit confusing.
You say you want to find 2605, but in the code snippet you look for 2036.
Maybe it is a typo, but it also COULD be why your code doesn't work.

The simple way:
C#
string s = File.ReadAllText(pathToINIFile);
if (s.Contains("2036="))
   {
   // Found it...
 
Share this answer
 
Comments
Jammes_Ca 26-May-16 14:13pm    
hi thanks for the answer but me i want just the number 2036 not 2036=
OriginalGriff 26-May-16 14:21pm    
And you can't work out how to remove the equals sign?
I'm tempted to suggest that this is the wrong career for you... :sigh:
koolprasad2003 27-May-16 2:37am    
Look OriginalGriff,
How people expect EXACT output from us and do not take effort to replace = sign :)
koolprasad2003 27-May-16 2:36am    
Just replace '2036' with your input number, do not remove "=" sign, as your number may repeat in other place.
Try this:
C#
string value = File.ReadAllText(@"C:\file.txt");

if (value.Contains("2036"))
{
  System.Console.WriteLine("trouvé");
}
 
Share this answer
 
Comments
Jammes_Ca 26-May-16 14:14pm    
big thanks it works :)
RickZeeland 26-May-16 14:16pm    
Then my solution is: just rename your .ini files to .txt files !
Alternatively, a version that doesn't require you to read the whole file into memory at once:
C#
foreach (string line in File.ReadLines(pathToFile))
{
    if (line.Contains("2036"))
    {
        Console.WriteLine("trouvé");
        break; // No need to keep searching
    }
}

File.ReadLines Method (String) (System.IO)[^]
 
Share this answer
 
You could use Windows API to read structured INI file
VB
Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer


Your file must structured like this:
[SECTION1]
2036=kl

[SECTION2]
2000=ss



And then use:
VB
 Public Const MAX_ENTRY As Integer = 32768
[...]
 Dim sSection as string = "SECTION1"
 Dim sParameteras string = "2036"
 Dim sDefVal as string = ""
 Dim sb As New StringBuilder(MAX_ENTRY)
 Dim iRet As Integer = GetPrivateProfileString(sSection, sParameter, sDefVal, sb, MAX_ENTRY, sFile)

 Debug.print (sb.ToString())


The code is in VB but you could do in c# on same way
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900