简体   繁体   English

使用 WinSCP 和 C# 匹配不区分大小写的文件夹名称

[英]Case insensitive folder name matching using WinSCP and C#

I am working on a project where I am using WinSCP .NET component for accessing an SFTP host.我正在一个项目中使用 WinSCP .NET 组件来访问 SFTP 主机。 The functionality of the code is to navigate to a folder in the SFTP and checks whether the provided file name is located inside the folder.代码的功能是导航到 SFTP 中的文件夹并检查提供的文件名是否位于文件夹内。

I have the below code and everything is working fine.我有以下代码,一切正常。 The below code is checking whether the folder exists in the SFTP remote path.下面的代码正在检查该文件夹是否存在于 SFTP 远程路径中。 If it's there I am iterating through all the files in the sFTP directory to check whether it has the filename that I provided.如果它在那里,我将遍历 sFTP 目录中的所有文件以检查它是否具有我提供的文件名。

string FolderName = row["Folder_Name"].ToString();
string FileName = row["File_Name"].ToString();

FileName = FileName.Replace("\r\n", string.Empty);

string remotePath = HomeDirectory + FolderName + "/";

if(session.FileExists(remotePath)) 
{
    RemoteDirectoryInfo directory = session.ListDirectory(remotePath);
    sFTPFiles.AddRange(directory.Files
        .Where(fileInfo => !fileInfo.IsDirectory)
        .Select(fileInfo => fileInfo.Name.ToString()));
    if(CheckFileExists(FileName, sFTPFiles) != FileExistence.EXISTS)
    {
        isDiscrepancy = true;
    } else
    {
        row["sftp_available"] = true;
        row["discrepancy_on_sftp"] = false;
    }
}

The problem that I am facing is the folder name case sensitive issues.我面临的问题是文件夹名称区分大小写的问题。 In my database the folder names are stored in Upper case.在我的数据库中,文件夹名称以大写形式存储。 But in the SFTP the same folder names has different case sensitive name ie.但在 SFTP 中,相同的文件夹名称具有不同的区分大小写的名称,即。 Upper Case or Sentence case or Lower Case.大写或句子大小写或小写。 Something like folderNAME , FOLDERNAME , foldername , fOLDERNAME类似folderNAMEFOLDERNAMEfoldernamefOLDERNAME

When the if(session.FileExists(remotepath) is getting executed, it gets failed due to the case sensitive issue. When my Foldername that I am verifying is FOLDERNAME and the same in SFTP is written as FOLDERname it is getting skipped due to case sensitivity.if(session.FileExists(remotepath)被执行时,由于区分大小写的问题,它会失败。当我正在验证的文件夹名是FOLDERNAME并且在 SFTP 中的相同写为FOLDERname时,由于区分大小写,它会被跳过.

Hence can anyone let me know how to check the folder names with non case sensitive method using WinSCP and C#.因此,任何人都可以让我知道如何使用 WinSCP 和 C# 以不区分大小写的方法检查文件夹名称。 All I need is if my Foldername in my database is FOLDERNAME , I want to navigate inside that folder in SFTP even though it is in any of the mentioned formats.我所需要的只是如果我的数据库中的文件夹FOLDERNAME Foldername我想在 SFTP 中的该文件夹内导航,即使它是上述任何一种格式。

Thanks in advance for your help.在此先感谢您的帮助。

The SFTP file checking API follows the conventions of the remote system. SFTP 文件检查 API 遵循远程系统的约定。 So if the remote system is case-sensitive, the check will be case-sensitive.因此,如果远程系统区分大小写,则检查将区分大小写。 You cannot do anything about it.你对此无能为力。

All you can do is to retrieve complete remote directory listing and do case-insentitive search yourself.您所能做的就是检索完整的远程目录列表并自己进行不区分大小写的搜索。


For quick and dirty case-insensitive replacement of Session.FileExists , use:要快速且不区分大小写地替换Session.FileExists ,请使用:

if (session.ListDirectory(HomeDirectory).Files.Any(
        file => file.Name.Equals(FolderName, StringComparison.OrdinalIgnoreCase)))
{
    // ...
}

Though obviously, this will be ineffective, if you are going to check lot of files/subfolders in the same folder.虽然很明显,如果您要检查同一文件夹中的大量文件/子文件夹,这将是无效的。 It that case, you better cache the listing and reuse it.在这种情况下,您最好缓存列表并重用它。

RemoteDirectoryInfo dirInfo = session.ListDirectory(HomeDirectory);
if (dirInfo.Files.Any(
        file => file.Name.Equals(FolderName, StringComparison.OrdinalIgnoreCase)))
{
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM