【问题标题】:Comparing file name strings in different folders比较不同文件夹中的文件名字符串
【发布时间】:2012-08-09 20:47:04
【问题描述】:

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

一旦选择了所有 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")。

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

谁能帮助我或给我一个如何提取子字符串的例子?

【问题讨论】:

    标签: c# string string-comparison


    【解决方案1】:

    Path 类内置了这个 - GetFileName 方法。

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 知道有点晚了,但是谢谢,这正是我所需要的,得到这个名字正是我想要的,但我不知道怎么做。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      相关资源
      最近更新 更多