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

  • SEARCH
  • Home
  • 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 9178779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:37:58+00:00 2026-06-17T17:37:58+00:00

I am trying to read a file (which works perfectly), the only problem is

  • 0

I am trying to read a file (which works perfectly), the only problem is that when there is a space in the path, the code crashes and tells me there is no such path. Does anyone know how to escape these spaces or another solution? Thanks!

Here is the code I have:

public static string ReadValue(string value)
        {
            try
            {
                FileStream propertiesFile = new FileStream(ServerLocation + FileName, FileMode.Open);
                StreamReader sr = new StreamReader(propertiesFile);
                string Line = sr.ReadLine();
                while (Line != null)
                {
                    if (Line.Contains(value))
                    {
                        var setting = Line.Split('=')[1];
                        Console.WriteLine(setting);
                        sr.Close();
                        return setting;
                    }
                    Line = sr.ReadLine();
                }
                sr.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine("Cannot find the specified file.");
                Console.WriteLine(e.ToString());
                return null;
            }
            return null;
        }
  • 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-06-17T17:37:59+00:00Added an answer on June 17, 2026 at 5:37 pm

    Normally, a path with whitespaces should not pose problems "C:\My Directory\Files" should be absolytely ok… maybe you are forgetting slashes somewhere in the begin of ServerLocation or at the end of FileName.

    String path = Path.Combine(ServerLocation, FileName);
    

    Or:

    FileStream propertiesFile = new FileStream(ServerLocation.Trim() + FileName.Trim(), FileMode.Open);
    

    Also, as suggested in my comment, change your loop to:

    while ((Line = sr.ReadLine()) != null)
    {
        if (Line.Contains(value))
        {
            var setting = Line.Split('=')[1];
            Console.WriteLine(setting);
            sr.Close();
            return setting;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a file line by line, which works perfectly but I
I am trying to write some simple code which will read a text file
I am trying to read a html file which is a plain page with
I am trying to read the file with the following format which repeats itself
I have an xml file(from federal government's data.gov) which I'm trying to read with
I am trying to read a file of over 1GB (which has 1,157,421,364 bytes),
I'm trying to read a file which is encoded in ISO-8859(ansi), and it contains
I am trying to read file headers using java, I want to get file
I am trying to read file and split its line to get some context(Computer
I'm trying to read a file in my maven project at /src/main/resources/file.txt. I'm using

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.