子字符串文件路径

本文关键字:路径 文件 字符串 | 更新日期: 2023-09-27 18:13:18

嗨,我试图在文件路径进入字典之前将其子字符串化。我试图声明起点,但给出了一个错误:

StartIndex不能大于length of string。参数名称:startIndex

这是我的代码

private  Dictionary<string,int> CreateDictionary(log logInstance)
        {
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++)
            {
                logLogentry entry = logInstance.logentry[entryIdex];
                for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++)
                {
                    logLogentryPath path = entry.paths[pathIdex];
                    string filePath = path.Value;
                    filePath.Substring(63);
                    string cutPath = filePath;
                    if (dictionary.ContainsKey(cutPath))
                    {
                        dictionary[cutPath]++;
                    }
                    else
                    {
                        dictionary.Add(cutPath, 1);
                    }
                }
            }
            return dictionary;
        }

任何帮助都太好了。

我也试过做

filePath.Substring(0, 63);

filePath.Substring(63, length);

子字符串文件路径

c#中的字符串是不可变的(一旦创建了字符串就不能修改),这意味着当您设置string cutpath = filepath时,您将cutpath的值设置为path.Value,因为您没有将filepath.SubString(63)的值分配给任何东西。修改

string filePath = path.Value;
filePath.Substring(63); // here is the problem
string cutPath = filePath;

string filePath = path.Value;
string cutPath = filePath.Substring(63);

First:

// It's do nothing
filePath.Substring(63);
// Change to this
filePath = filePath.Substring(63);
第二:

63是我要从每个文件路径中提取的字符条目看起来像这样:/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax标记开膛手v1与最后比特的真实信息,我想成为/DataModifier.cs

用63是个坏主意。最好找到最后一个"/"并将其位置保存到某个变量

谢谢大家的帮助

我的代码现在工作了,看起来像这样:

Private  Dictionary<string,int> CreateDictionary(log logInstance)
    {
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++)
        {
            logLogentry entry = logInstance.logentry[entryIdex];
            for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++)
            {
                logLogentryPath path = entry.paths[pathIdex];
                string filePath = path.Value;
                if (filePath.Length > 63)
                {
                    string cutPath = filePath.Substring(63);
                    if (dictionary.ContainsKey(cutPath))
                    {
                        dictionary[cutPath]++;
                    }
                    else
                    {
                        dictionary.Add(cutPath, 1);
                    }
                }
            }
        }
        return dictionary;
    }

阅读您的评论,似乎您只是想从文件路径中获取文件名。有一个内置的实用程序可以在任何路径上实现这一点。

从MSDN:

路径。GetFileName

返回指定路径字符串的文件名和扩展名。

https://msdn.microsoft.com/en-us/library/system.io.path.getfilename%28v=vs.110%29.aspx

这里是一个代码示例,让您开始。

string path = @"/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1/Help_Document.docx";
string filename = System.IO.Path.GetFilename(path);
Console.WriteLine(filename);

Help_Document.docx

下面是修改后的代码;

Private  Dictionary<string,int> CreateDictionary(log logInstance)
    {
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++)
        {
            logLogentry entry = logInstance.logentry[entryIdex];
            for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++)
            {
                logLogentryPath path = entry.paths[pathIdex];
                string filePath = path.Value;
                // extract the file name from the path
                string cutPath = System.IO.Path.GetFilename(filePath);
                if (dictionary.ContainsKey(cutPath))
                {
                    dictionary[cutPath]++;
                }
                else
                {
                    dictionary.Add(cutPath, 1);
                }
            }
        }
        return dictionary;
    }