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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:29:22+00:00 2026-05-26T17:29:22+00:00

I am checking if a file exists, and if it does, I put it

  • 0

I am checking if a file exists, and if it does, I put it in a list, otherwise I remove from the list. My code is so:

foreach (KeyValuePair<string, string> kvp in dict)
{
    _savedxml.Add(kvp.Key.ToString());
}

string namewithext=null;
for (int i = 0; i < _savedxml.Count; i++)
{
    namewithext = string.Concat(_savedxml[i], ".xml");
    System.IO.FileInfo file_info = new System.IO.FileInfo((string)namewithext);
    long size = file_info.Length;
    if (size == 0)
    {
        _savedxml.RemoveAt(i);
    }
}

for (int i = 0; i < _savedxml.Count; i++)
{
    if (System.IO.File.Exists(System.IO.Path.GetFullPath(namewithext)))
    {
    }
    else
    {
        _savedxml.Remove(namewithext);
    }
}

I’ve tried many ways, but even though a file does not exist, the list contains it. I’ve probably made a silly error.

How can I do this?

  • 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-26T17:29:22+00:00Added an answer on May 26, 2026 at 5:29 pm

    There are several errors in the code:

    • You set the namewithext variable for each item in the first loop, then use it in the second loop, so you will be checking if the last file exist over and over.

    • When you remove an item, the next item takes its place in the list, so you will skip the check for the next item.

    • You are checking the length of the files before checking if the files exist, so you will get a FileNotFoundException when you try to get the length for files that doesn’t exist.

    Corrected (and some cleanup):

    foreach (KeyValuePair<string, string> kvp in dict) {
      _savedxml.Add(kvp.Key);
    }
    
    for (int i = _savedxml.Count - 1; i >= 0 ; i--) {
      string namewithext = _savedxml[i] + ".xml";
      if (!System.IO.File.Exists(System.IO.Path.GetFullPath(namewithext))) {
        _savedxml.RemoveAt(i);
      }
    }
    
    for (int i = _savedxml.Count - 1; i >= 0 ; i--) {
      string namewithext = _savedxml[i] + ".xml";
      System.IO.FileInfo file_info = new System.IO.FileInfo(namewithext);
      if (file_info.Length == 0) {
        _savedxml.RemoveAt(i);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are you aware of any serious performance problems with checking if a file exists
I have a large number of files in a .tar.gz archive. Checking the file
Hey all, i am checking for an internet connection by checking for a file
I'm storing SQL database backup file (*.bak) in the Visual Studio solution and checking
i'm working with TFS and i need to edit file localy without checking it
In Java, I just read a file into a ByteBuffer. When I started checking
I'm checking to see if a folder exists by using the file_exists function. If
how do i disable an html element after checking in php if it exists?
I'm checking whether a file has been modified or not in order to refresh
I want to download a file from server to a local host. i have

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.