G'day all,
I'm currently a little stuck with my application. The strings i'm working with are a filepath and then a filename, what I want is to compare the filename with the filepath and then output the directory that most matches with the filename. My current code below works like the following:
Filepath: C:\Program Files\ABCDE Folder\system
Filename: ABCDE.exe
Return: ABCDE Folder
However it wouldn't work if the first few characters don't match, for example:
Filepath: C:\Program Files\ABCDE Folder\system
Filename: ACDE.exe or AAABBBCCDDEE.exe
Return: null
What i'm aiming for is to find out a way to get some sort of percentage of "accuracy" of the filename to any given directory within the filepath. Thanks again for any input. :D
public string FindProgramName(string FullPath)
{
string filename;
string PathuntilFile;
filename = Path.GetFileNameWithoutExtension(FullPath);
PathuntilFile = Path.GetDirectoryName(FullPath);
filename = filename.ToLower();
string[] pathComponents = PathuntilFile.ToLower().Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string comp in pathComponents)
{
int matches = 0;
for (int i = 0; i < comp.Length && i < filename.Length; i++)
{
if (comp[i] == filename[i])
{
if (++matches == 3)
return comp;
}
else
{
break;
}
}
}
return "";
}