C# .PadLeft未正确插入填充字符

C# .PadLeft未正确插入填充字符,c#,winforms,C#,Winforms,我一直在尝试为一个文件显示工具编写一段代码。我正在尝试使用.padlift移动字符以替换目录文本 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 count

我一直在尝试为一个文件显示工具编写一段代码。我正在尝试使用.padlift移动字符以替换目录文本

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;
输入是驱动器上的一个目录。上面的代码段是读取文件并将其放入列表的代码的一部分

想要的产出

Z:\Documents
            \~~Manifest.txt
            \Anti-Tau.png
            \Anti-Tau.rosz
            \Army.png
实际产量

Z:\Documents
\~~Manifest.txt
\Anti-Tau.png
\Anti-Tau.rosz
输出不反映我正在填充临时字符串的左侧。我尝试更改临时字符串,但似乎没有任何作用


我一直在观看VS2012的“本地人”窗口,这似乎是唯一一个没有按预期工作的窗口。

您需要将该值重新分配给临时值:

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;
}
public List GetFileList(字符串路径)
{   
整数计数=0;
var temp1=Directory.GetFiles(路径);
List temp2=新列表();
foreach(temp1中的字符串文件)
{
字符串temp=“\\”;
int padCount=(path.Length+file.Length+temp.Length+1);//+1,因为它只将temp计算为一个字符。
温度=左焊盘温度(焊盘计数“-”);
Add(temp+Path.GetFileName(temp1[count]);
计数++;
}
返回temp2;
}
试试这个:

            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));
            }
String path=“D:\\”;
List temp1=新列表();
List temp2=新列表();
AddRange(Directory.GetFiles(path));
控制台写入线(路径);
foreach(temp1中的字符串文件)
{
字符串temp=“\\”;
int padCount=path.Length+temp.Length;//(path.Length+file.Length+temp.Length+1);//+1,因为它只将temp计算为一个字符。
温度=左焊盘温度(焊盘计数“-”);
添加(temp+Path.GetFileName(文件));
Console.WriteLine(temp+Path.GetFileName(文件));
}

请显示输入并要求输出。@TAHASULTANTEMURI我添加了所需的输入和输出。我还添加了实际获得输出的内容。您是否正在尝试使用填充将文件列表打印到控制台?@prospector否,它会写入列表,然后发生一些其他事情,然后打印到文件。这真的是我错过的吗?而且。。。这是我错过的。感觉真的很愚蠢,我不知道为什么我认为我不需要这样做。谢谢你的工作方法,我不需要它,因为我已经有了整个程序的工作,除了填充。不过,对于其他需要它的人来说,它仍然很有用。