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 8369573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:41:00+00:00 2026-06-09T13:41:00+00:00

Is there a way to store the content of a file as string or

  • 0

Is there a way to store the content of a file as string or as a dictionary instead of just its file path/name?

Below is the method that I am currently using for getting the file path from a Windows Form. Is there a way to adapt it or should I start from scratch? I am loading .ini files which is only text. LINQ seems to be one route but I am not familiar with it.

public void ShowSettingsGui()
{
    System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
    ofd.Multiselect = false;
    ofd.Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*";
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string[] filePath = ofd.FileNames;
    }
    m_settings = Path.GetDirectoryName(ofd.FileName);
}
  • 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-09T13:41:01+00:00Added an answer on June 9, 2026 at 1:41 pm

    LINQ is indeed a nice way to do it: We simply convert the paths to a dictionary (where they become the keys). The values are determined by calling File.ReadAllText on every file path.

    var dialog = new OpenFileDialog() { Multiselect = true, 
        Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    var paths = dialog.FileNames;
    var fileContents = paths.ToDictionary(filePath => filePath, File.ReadAllText);
    

    To help you understand what’s going one here, take a look at the (roughly equivalent) non-LINQ version. Here, we explicitly iterate over the FileNames and add them as keys to our dictionary while again calling File.ReadAllText on every one of them.

    // same as first snippet without the last line
    foreach (var filePath in paths)
    {
        fileContents.Add(filePath, File.ReadAllText(filePath));
    }
    

    Set a breakpoint to the last line of each snippet, run them and take a look at the contents of the dictionary to determine the result.

    EDIT: It wasn’t clear in the question, but it seems you’re only interested in a single file name. That means you don’t need LINQ at all (m_settings needs to be a string).

    var dialog = new OpenFileDialog{Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"};
    if (dialog.ShowDialog() != DialogResult.OK) return;
    m_settings = File.ReadAllText(dialog.FileName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to download a large file using Go that will store
is there any way to store duplicate keys in a dictionary? I have a
Is there a way to store the state of objects in GXT? That is,
Is there any way to store file creation time in linux? May be filesystem
is there a way to store a class name in localstorage ?? If there
Is there a JavaScript way to store data that is unique to a browser
Is there any way to store a session variable in OpenEdge SQL similar to
Is there any way to store the generic parameter type passed in at construction
Is there a way to store values on client side permanently? I have a
is there any easy way of store XML data into core data? Currently, my

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.