1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Text File help required in c# windows application

Discussion in 'C#' started by digiface, Sep 26, 2013.

  1. #1
    I am working with .txt files in C#, I want a functionality that if any text already exist in my .txt file, it should not write it.
    SEMrush
    Can somebody Please help me?

    Thank you
     
    digiface, Sep 26, 2013 IP
    SEMrush
  2. grandseo

    grandseo Greenhorn

    Messages:
    39
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    23
    #2
    Its pretty simple you can use following code for it

    var filename = @"c:\test.txt";
    var textToWrite = "your text here";
    string fileContents;


    // Get File Text
    using (var sr = new StreamReader(filename))
    {
    fileContents = sr.ReadToEnd();
    }


    // Write to File
    using (var sw = new StreamWriter(filename, true))
    {
    // check if text already exists in file
    if (!fileContents.Contains(textToWrite))
    {
    sw.WriteLine(textToWrite); // or use sw.Write depending on the usage scenario
    }
    }
     
    grandseo, Sep 26, 2013 IP
    seotraining and modernlifeisadam like this.