我一直在尝试为文件清单工具编写一段代码。我试图使用.PadLeft来移动字符来替换目录文本。
int count = 0;
temp1.AddRange(Directory.GetFiles(path));
foreach (string file in temp1)
{
string temp = "\\";
int padCount = (path.Length + file.Length + temp.Length + 1); //+1 because it counts temp as only one character.
temp.PadLeft(padCount, '-');
temp2.Add(temp + Path.GetFileName(temp1[count]));
count++;
}
return temp2;
输入是驱动器上的目录。上面的代码段是读取文件的代码的一部分,并将它们放在List<>中。
通缉输出
Z:\Documents
\~~Manifest.txt
\Anti-Tau.png
\Anti-Tau.rosz
\Army.png
实际输出
Z:\Documents
\~~Manifest.txt
\Anti-Tau.png
\Anti-Tau.rosz
输出并不反映我填充临时字符串的左边。我试图改变临时字符串,但似乎没有做任何事情。
我一直在关注VS2012的本地人窗口,这似乎是唯一没有按预期工作的东西。
答案 0 :(得分:1)
您需要将值分配回temp:
temp = temp.PadLeft(padCount, '-');
这是一个完整的工作方法:
public List<string> GetFileList(string path)
{
int count = 0;
var temp1 = Directory.GetFiles(path);
List<string> temp2 = new List<string>();
foreach (string file in temp1)
{
string temp = "\\";
int padCount = (path.Length + file.Length + temp.Length + 1); //+1 because it counts temp as only one character.
temp = temp.PadLeft(padCount, '-');
temp2.Add(temp + Path.GetFileName(temp1[count]));
count++;
}
return temp2;
}
答案 1 :(得分:0)
试试这个:
String path = "D:\\";
List<String> temp1 = new List<string>();
List<String> temp2 = new List<string>();
temp1.AddRange(Directory.GetFiles(path));
Console.WriteLine(path);
foreach (string file in temp1)
{
string temp = "\\";
int padCount = path.Length+temp.Length;//(path.Length + file.Length + temp.Length + 1); //+1 because it counts temp as only one character.
temp=temp.PadLeft(padCount, '-');
temp2.Add(temp + Path.GetFileName(file));
Console.WriteLine(temp + Path.GetFileName(file));
}