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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:30:53+00:00 2026-05-23T17:30:53+00:00

Here is my program: protected int CheckExisting(string item_id) { StreamReader sr = new StreamReader(@D:\ItemID.txt);

  • 0

Here is my program:

protected int CheckExisting(string item_id)
{
            StreamReader sr = new StreamReader(@"D:\ItemID.txt");
            string line = sr.ReadLine();

            while (line != null)
            {
                if (0 == string.Compare(line, item_id))
                    return 1;

                line = sr.ReadLine();
            }

            sr.Close();
            return 0;
}

protected void WriteNewLog(string item_id)
{
            using (StreamWriter sw = File.AppendText(@"D:\ItemID.txt"))
            {
                sw.WriteLine(item_id);
            }
}

protected void xHandler(int num)
{
    for(int i= 0; i< num; i++)
        if (0 == CheckExisting(item_id))
        {                       
            WriteNewLog(item_id);
        }
}

When o run the program, an exception unhandled occur: “The process cannot access the file ‘D:\ItemID.txt’ because it is being used by another process.” Can u guys help me fix that? Thanks so much!

  • 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-23T17:30:54+00:00Added an answer on May 23, 2026 at 5:30 pm

    If this executes:

    if (0 == string.Compare(line, item_id))
        return 1;
    

    then you won’t close your StreamReader. Use a using block when reading as well as writing.

    Additionally:

    • I’d suggest using bool instead of an integer to indicate yes/no results
    • I’d use a simple equality check instead of calling Compare and checking the results against 0
    • If you’re using .NET 4, File.ReadLines is a simpler way of reading lines
    • LINQ makes it easier to implement CheckExisting to start with
    • The name xHandler doesn’t follow .NET naming conventions
    • Underscores in parameter names don’t follow .NET naming conventions
    • I would suggest using braces for clarity everywhere, even where they’re not strictly necessary
    • I would put the constants in comparisons at the end of the comparison instead of at the start; I believe most people find that more readable
    • There’s no indication that the CheckExisting and WriteNewLog methods need to be protected rather than private
    • You’re not actually using your i variable in xHandler

    Here’s the implementation I’d use:

    const string FileName = @"D:\ItemID.txt";
    
    private bool CheckExisting(string itemId)
    {
        return File.ReadLines(FileName)
                   .Contains(itemId);
    }
    
    private void WriteNewLog(string itemId)
    {
        using (TextWriter writer = File.AppendText(FileName))
        {
            writer.WriteLine(itemId);
        }
    }
    
    // Adjust name appropriately
    protected void FooHandler(int num)
    {
        for (int i = 0; i < num; i++)
        {
            // Probably use i here somewhere?
            if (!CheckExisting(itemId))
            {
                WriteNewLog(itemId);
            }  
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my program so far: int main() { char choice = 'D'; string inputString;
Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
Here is a quick test program: public static void main( String[] args ) {
new here, I'm working on a program for port scanning, TCP works well, but
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
there is this check-in-out program here at my workplace, it only takes the data
I've got a Schroedinger's Cat type of problem here -- my program (actually the
Okay here's the program I have typed up(stdio.h is included also): /* function main
Thanks to pbos help and its program (published here , for xoring a big
Here is a little test program: #include <iostream> class Test { public: static void

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.