-3

I have folder with these files:

  • file1.exe
  • file2.dll

I want to check is exists extraneous files in this folder. For example if I create examplefile.exe in this folder it must give me an error, there must be only that files which listed above. So i've created needed files string:

string[] only_these_files = { 
    "file1.exe", 
    "file2.dll"
};

Now I need to search for extraneous files, but how to? Thanks immediately.

I'm tried this code, but I don't know how to check it.

string[] only_these_files = { 
            "image1.png", 
            "image2.png", 
            "image3.png",
            "image4.png",
            "image5.png"
        };
        string[] fileEntries = Directory.GetFiles(@"C:\Users\Dewagg\Desktop\test\");

        List<String> badFiles = new List<string>();

        foreach (string fileName in fileEntries)
            if (!only_these_files.Contains(fileName))
            {
                badFiles.Add(fileName);
            }
Dewagg
  • 145
  • 10
  • 2
    Don't ask the community to write code for you. Ask for assistance in fixing your own code. – oppassum Jul 10 '15 at 17:24
  • 1
    Break the problem in to parts then come back here with the specific part you don't know how to do: 1) Get a list of all files in a folder 2) See if any files in a list of files are not one of the two files that are allowed 3) Report a error message to the user. – Scott Chamberlain Jul 10 '15 at 17:24
  • `I'm tried this code, but I don't know how to check it.`: What do you mean? – sstan Jul 10 '15 at 17:29
  • @sstan, idk how to check is exists extraneous files – Dewagg Jul 10 '15 at 17:30
  • @Dewagg: What's wrong with your current code. It doesn't work? If not, please describe what is wrong with it. Be specific. – sstan Jul 10 '15 at 17:33
  • @sstan, if I change "badFiles.Add(fileName);" to "MessageBox.Show(filename)" it prints all files which exists in that folder, but I wan't to check is exists **extraneous** files – Dewagg Jul 10 '15 at 17:35
  • @Dewagg: Sorry to keep insisting, but can you post an example of a filename that you are getting back that you should not be getting? Is it the ***exactly*** the same as one of the filenames in your `only_these_files` array? – sstan Jul 10 '15 at 17:39

3 Answers3

4

If you want to check your code, then you can always put a breakpoint in it and watch the execution. You'd want to create files on your desktop so that you know the expected result.

If you want to verify that there aren't any bad files then you can check the size of the bad files list.

So you'd want something like:

if(badFiles.Count>0)//based off your sample code with png's
{
     //notify user
     MessageBox.Show("Bad files were found"); //or create anonymous function to display bad files
     // or Console.WriteLine("Bad files were found");
}
Speerian
  • 1,138
  • 1
  • 12
  • 29
1

It's not exactly rocket science: something like this would do you:

HashSet<string> allowedFiles = new HashSet<string>( StringComparer.OrdinalIgnoreCase )
{
  "file1.exe" ,
  "file2.dll" ,
};
DirectoryInfo directory = new DirectoryInfo( @"c:\foo\bar" ) ;

bool containsNonAllowedFiles = directory
                               .EnumerateFiles( @"C\foo\bar" )
                               .Any( fi => !allowedFiles.Contains( fi.Name ) )
                               ;
bool containsAllAllowedFiles = allowedFiles
                               .All( fn => directory.EnumerateFiles( fn ).Any() )
                               ;
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
0

try this since you only need to check two files, its a bad coding convention to use a arraylist for what you're doing btw

try{
   if (!File.Exists("TextFile1.txt"))
     throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
   // your message here.
}
Huang Chen
  • 1,177
  • 9
  • 24
  • From my understanding OP doesn't just want to check to see if the file exists, but to also make sure that there are not any other files in that directory. – Speerian Jul 10 '15 at 17:28
  • `System.IO.Directory myDir = GetMyDirectoryForTheExample(); int count = myDir.GetFiles().Length;` then he can just check the count is 2 – Huang Chen Jul 10 '15 at 17:29
  • I'd rather see a List then direct comparisons of a series of individual items any day... It can be expanded. – Austin T French Jul 10 '15 at 17:31
  • Nooooooope, I want to write only allowed files, and if system founds extraneous file to show message to me (sorry, I'm not english) – Dewagg Jul 10 '15 at 17:31
  • I would to but hes a beginner, a quick hard fix should be ok I guess – Huang Chen Jul 10 '15 at 17:31