Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6202249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:41:58+00:00 2026-05-24T04:41:58+00:00

I wrong a function to read a configuration file, but if the command line

  • 0

I wrong a function to read a configuration file, but if the command line arguement “-ip x.x.x.x” is specified, I want to overwrite the IP setting in the configuration file. I am using the following code which reads fine, but appends my new line to the end. How can I have it rewrite the line it is reading?

    private static void ParsePropertiesFile(string file)
    {
        using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            StreamReader sr = new StreamReader(fs);
            StreamWriter sw = new StreamWriter(fs);

            string input;

            while ((input = sr.ReadLine()) != null)
            {
                // SKIP COMMENT LINE
                if (input.StartsWith("#"))
                {
                    continue;
                }
                else
                {
                    string[] line;
                    line = input.Split('=');

                    if (line[0] == "server-ip")
                    {
                        // If IP was not specified in the Command Line, use this instead
                        if (ServerSettings.IP == null)
                        {
                            // If the setting value is not blank
                            if (line[1] != null)
                            {
                                ServerSettings.IP = IPAddress.Parse(line[1]);
                            }
                        }
                        else
                        {
                            sw.("--REPLACE_TEST--");
                            sw.Flush();
                        }
                    }
                }
            }
        }
    }

It makes sense why it appends to the end, but I can’t think of any way to just rewrite the line, since the IP string might be longer than what is currently there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:42:00+00:00Added an answer on May 24, 2026 at 4:42 am

    An easier way is to read all the lines and replace the line(s) you want to replace, then append the new lines to file again like:

    string[] lines = File.ReadAllLines("Your file path");
    
    for  (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
    {
        if (/*if we want to modify this line..*/)
        {
            lines[lineIndex] = "new value";
        }
    }
    
    File.AppendAllLines(lines);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc 4.4.1 I am using the read function to read in a wave file.
What is wrong with this function... The function intends to remove the specified leading
When I click a link which calls following function...I get Something went wrong. We're
I am trying to read the following file (The comments are not there in
I have a C++ File class with read function, that is supposed to read
I'm new to C and am trying to use the read function. I want
I'm using the following code to try to read an input from user and
I was making a function to read a file containing some dumped data (sequence
I've a very simple function that should read a txt file and return all
Not sure if this is intended behavior or a bug or a wrong function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.