2012-08-13 181 views
0

我正在尝试编写一些代码,用于检查用户选择的模糊图片是否与普通对应文件相同。在不同文件夹中比较文件名字符串

它使用图像位置字符串来比较每个选择一旦所有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 [](即,如果” ......文件路径不同的目录/cat.jpg“==”... filepath/Blurred/cat.jpg“)。

我在想子串是这里的解决方案,但我不知道如何正确使用它们。

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

回答

3

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

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

if(fileone == filetwo) doStuff(); 
+1

知道这有点晚,但谢谢,这正是我所需要的,得到的名字就是我想要做的,但我不知道如何。 – Jamz 2012-08-30 11:03:21

1

Path类有这个建于 - 在GetFileName方法。

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