您好我正在尝试在文件路径进入字典之前对其进行子串。我试图声明起点但是给出了一个错误:
StartIndex不能大于字符串的长度。参数名称:startIndex
这是我的代码
$(msg)
任何帮助都会很棒。
我也尝试过做
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);
答案 0 :(得分:2)
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);
答案 1 :(得分:0)
首先:
// It's do nothing
filePath.Substring(63);
// Change to this
filePath = filePath.Substring(63);
第二
63是我从每个文件路径子串的字符 条目如下所示:/ GEM4 / trunk / src / Tools / TaxMarkerUpdateTool / Tax Marker Ripper v1,最后一位是我想要的真实信息 显示的是:/DataModifier.cs
使用63是一个坏主意。最好找到最后一个“/”并将其位置保存到某个变量。
答案 2 :(得分:0)
感谢大家的帮助
我的代码现在有效,看起来像这样:
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;
}
答案 3 :(得分:0)
阅读您的评论,您似乎只需要文件路径中的文件名。有一个内置的实用程序可以在任何路径上实现这一点。
来自MSDN:
<强> Path.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;
}