简体   繁体   中英

C# how to check if file is in folder according to object of filenames

I have the following method:

public static Boolean CheckContents(string ExportDirectory, string FileName, string DspFleName, String RteFleName, string FulRteName, string EqpFleName, int CompanyId, string CompanyName)
    {

        if (DspFleName != "None")
        {

           IList<string> DspFle= DspFleName.Split(',');
           IList<string> ActualFiles = Directory.GetFiles(ExportDirectory);
            for (int i = 0; i < DspFle.Count; i++)
            {
                if (DspFle[i] != ActualFiles[i])
                {
                    return false;
                }
            }
        }
        return true;
    }
}

Basically what this code is meant to do is get all file names from the DspFle field which is seperated by a ,. So this would look like so:

test.txt,test2.csv

Then it is getting the acutal files in the directory that is specified from 'ExportDirectory' and returns those into an IList

I am having 2 problems here: 1.The Directory.GetFiles returns the whole file path so that will always return false. I also tried Path.GetFileNames and this only returns the file name but it does not return the extension.

2.I need to compare my entire DspFle to my ActualFile IList as the file names could be in different parts of the list.

Any ideas?

Your code expects not only for the file to exist, but to be in the same position...

Try this one instead :

    public static Boolean CheckContents(string ExportDirectory, string DspFleName)
    {
        if (DspFleName == "None")
            return true;

        var DspFle = DspFleName.Split(',');
        var ActualFiles = Directory.GetFiles(ExportDirectory);

        foreach(var file in DspFle)
            if (!ActualFiles.Any(x=>Path.GetFileName(x).Equals(file)))
                return false;

        return true;
    }
 List<String> fileNames = new List<String>();
 String[] files = Directory.GetFiles(".");
 foreach (String file in files)
 {
       fileNames.Add(System.IO.Path.GetFileName(file));
 }

That will return the filename with extensions. You can then compare to your IList at that point.

Why bother going through all the trouble of building two lists when you could just check if each file exists in the directory? Effectively that is what your code is doing anyway.

 foreach(string DspFle in DspFleName.Split(',')) {  
       string CheckPath = Path.Combine(ExportDirectory,DspFle[i]);
       if (!File.Exists(CheckPath)) return false;  
 }

 return true;

Maybe this is will do what you want?

        if (DspFle == "None")
            return true;

        List<string> DspFle = DspFleName.Split(',');
        List<string> ActualFiles = new List<string>();

        foreach (string file in Directory.GetFiles(ExportDirectory)
        {
            DirectoryInfo di = new DirectoryInfo(file);
            ActualFiles.Add(di.Name);            
        }

        foreach (string file in DspFle)
        {
            if (!ActualFiles.Contains(dspFile))
                return false;
        }

        return true;

DirectoryInfo will allow you to return the name of a file including the extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM