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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:45:03+00:00 2026-05-13T11:45:03+00:00

I have a file created from a directory listing. From each of item a

  • 0

I have a file created from a directory listing. From each of item a user selects from a ListBox, the application reads the directory and writes out a file that matches all the contents. Once that is done it goes through each item in the ListBox and copies out the item that matches the ListBox selection. Example:

Selecting 0001 matches:

  • 0001456.txt
  • 0001548.pdf.

The code i am using isn’t handling 0s very well and is giving bad results.

var listItems = listBox1.Items.OfType<string>().ToArray();
var writers = new StreamWriter[listItems.Length];
for (int i = 0; i < listItems.Length; i++)
{
    writers[i] = File.CreateText(
        Path.Combine(destinationfolder, listItems[i] + "ANN.TXT"));
}
var reader = new StreamReader(File.OpenRead(masterdin + "\\" + "MasterANN.txt"));

string line;
while ((line = reader.ReadLine()) != null)
{
    for (int i = 0; i < listItems.Length; i++)
    {
        if (line.StartsWith(listItems[i].Substring(0, listItems[i].Length - 1)))
            writers[i].WriteLine(line);
    }
}

Advice for correcting this?

Another Sample:
I have 00001 in my listbox: it returns these values:
00008771~63.txt
00002005~3.txt
00009992~1.txt
00001697~1.txt
00000001~1.txt
00009306~2.txt
00000577~1.txt
00001641~1.txt
00001647~1.txt
00001675~1.txt
00001670~1.txt

It should only return:
00001641~1.txt
00001647~1.txt
00001675~1.txt
00001670~1.txt
00001697~1.txt

Or if someone could just suggest a better method for taking each line in my listbox searching for line + “*” and whatever matches writes out a textfile…

  • 1 1 Answer
  • 4 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-13T11:45:03+00:00Added an answer on May 13, 2026 at 11:45 am

    This is all based pretty much on the one example you gave, but I believe the problem is that when you are performing your matching, you are getting the substring if your list item value and chopping off the last character.

    In your sample you are attempting to match files starting with “00001”, but when you do the match you are getting substring starting at zero and value.length-1 characters, which in this case would be “0000”. For example:

    string s = "00001";
    Console.WriteLine(s.Substring(0,s.Length-1));
    

    results in

    0000

    So I think if you just changed this line:

    if (line.StartsWith(listItems[i].Substring(0, listItems[i].Length - 1))) 
                writers[i].WriteLine(line);
    

    to this

    if (line.StartsWith(listItems[i]))
                writers[i].WriteLine(line);
    

    you would be in good shape.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a file named MyFile.db using SQLite3 from my C#.net application. This
I have successfully created an app that reads from a bundled .plist file and
I have a corpus, x, in R created from a directory using DirSource. Each
I have two HANDLEs and they are created from the same file, in such
I want to load a palette from a bitmap file I have created. The
I have created an .htaccess files that will prevent anyone from accessing a file
Accidentally I have created file - (just a minus) in a directory and commited
Possible Duplicate: Build Tar file from directory in PHP without exec/passthru Does PHP have
Say I have a file file.txt on my mainframe host xxx.xxx.xxx.xx created by user
i want to have a xml file in the user specifed directory. for that

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.