子字符串文件路径
Substring file path
您好,我正在尝试在文件路径进入字典之前对其进行子字符串化。我试图声明起点但出现错误:
StartIndex 不能大于字符串的长度。参数名称: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);
第一个:
// It's do nothing
filePath.Substring(63);
// Change to this
filePath = filePath.Substring(63);
第二个:
63 is the character I was to substring from the file path for every
entry looks like this: /GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax
Marker Ripper v1 with the final bit the real information i want to be
shown which is: /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:
Path.GetFileName
Returns the file name and extension of the specified path string.
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;
}
您好,我正在尝试在文件路径进入字典之前对其进行子字符串化。我试图声明起点但出现错误:
StartIndex 不能大于字符串的长度。参数名称: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);
第一个:
// It's do nothing
filePath.Substring(63);
// Change to this
filePath = filePath.Substring(63);
第二个:
63 is the character I was to substring from the file path for every entry looks like this: /GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1 with the final bit the real information i want to be shown which is: /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:
Path.GetFileName
Returns the file name and extension of the specified path string.
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;
}