I have created some code which pulls a list of file names off the server and stores them, I also get the folder names from a directory on my computer and I need to compare them both to find out if there are any names which are on the local machine and not on the server.
serverlistarray contains the list of folders on the server (1 folder per entry), local listarray contains the directory listing on the local computer, this is also has 1 folder per entry. I read the local files using this:
String[] localfilelistarray = System.IO.Directory.GetDirectories(/*file location*/);
My problem lies within the get directory's, it includes the full path of the file which I cannot compare. The code below is what I have tried to do, I check the serverlistarray for the file in locallistarray and if they match i put them in the list.
for (int b = 0; b < localfilelistarray.Length; b++)
{
//problem with the compare
if (!serverlistarray.Contains(localfilelistarray[b].Replace(/*file path before filename*/, "")))
{
//add to list variable
}
}
I then use this code to do what I want for everything in the list (for now its a message box to show the filename).
for (int f = 0; f < delete.Count(); f++)
{
MessageBox.Show(/*list variable*/);
}
BUT I get every file even if it is in the serverlistarray and I cant work out whats wrong. I believe its to do with the comparison of the two arrays but when I put message boxes in that loop thay come out as expected but the comparison doesnt seem to work.
Can anyone see what ive done wrong?
ive tried using replace and direct comparisons to no avail. I though of adding the file path to the serverlistarray BUT I can not do this as I need the raw file name for server operations and other parts of this code.
EDIT:
serverlistarray example:
[0]folder1
[1]folder2
[2]folder3
localfilearray example:
[0]c:\\users\Noliver\folder1
[1]c:\\users\Noliver\folder2
[2]c:\\users\Noliver\folder3
[3]c:\\users\Noliver\folder4
[4]c:\\users\Noliver\folder5
expected output (message boxes in for loop):
c:\\users\Noliver\folder4
&
c:\\users\Noliver\folder5
EDIT 2:
Ive tried this code with no lick using the file path:
for (int b = 0; b < localfilelistarray.Length; b++)
{
String[] temp = System.IO.Path.GetFileName(localfilelistarray[b]).Split('.');
MessageBox.Show(localfilelistarray[b] + " - " + temp[0]);
for (int t = 0; t < serverlistarray.Length;t++)
{
MessageBox.Show("server " + serverlistarray[t] + " - " + localfilelistarray[b] + " - " + temp[0]);
}
if (!update.Contains(temp[0]))
{
delete.Add(localfilelistarray[b]);
}
}
the output from this showed that the serverlistarray contained the file name from temp[0] (the file name with the extension taken off) but all the files were in the list variable. could this come down to new lines or a space at the end of one of the strings? for some reason the if statement checking if the array contains the value doesnt work.