比较不同文件夹中的文件名字符串

时间:2012-08-13 15:25:21

标签: c# string string-comparison

我正在尝试编写一些代码,用于检查用户选择的模糊图片是否与普通图片​​相同。

一旦选择了所有6个图片选项,它就使用图像位置字符串来比较每个选择。 到目前为止,它将普通图片作为文本文件中的图片位置数组,作为一种密码。

基本上,我想获取密码字符串数组中每个条目的图像位置字符串(文件名部分)的最后一部分,并将其与用户点击的模糊图片的当前选择(数组)进行比较

到目前为止,比较部分是这样的:

//Test to see whether the password entered is the same as the saved password.
if (passwordEntered[0] == passwordLocation[0] && passwordEntered[1] == passwordLocation[1] && passwordEntered[2] == passwordLocation[2])
{
      MessageBox.Show(TextResources.alertMessageText.passwordCorrect);
      Application.Exit();
}

但是,这不起作用,因为passwordEntered []的图像位置查找与passwordLocation []不同的目录(即如果“... filepath / cat.jpg”==“... filepath / Blurred / cat.jpg“)。

我认为子串是这里的解决方案,但我不确定如何正确使用它们。

任何人都可以帮助或给我一个如何提取子串的例子吗?

2 个答案:

答案 0 :(得分:3)

使用System.IO.Path.GetFileName(path)方法:

string fileone = GetFileName(pathone); // > fileone.ext
string filetwo = GetFileName(pathtwo); // > filetwo.ext

if(fileone == filetwo) doStuff();

答案 1 :(得分:1)

Path类内置了GetFileName方法。

if ( Path.GetFileName(pathOne) == Path.GetFileName(pathTwo) )