Reputation: 3
I have been attempting to write a piece of code for a file manifesting tool. I am attempting to use .PadLeft to shift the characters to replace the directory text.
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;
The input is a directory on the drive. The snippet above is part of the code that reads the files and puts them a in List<>.
Wanted Output
Z:\Documents
\~~Manifest.txt
\Anti-Tau.png
\Anti-Tau.rosz
\Army.png
Actual Output
Z:\Documents
\~~Manifest.txt
\Anti-Tau.png
\Anti-Tau.rosz
The output doesn't reflect that I am padding the left of the temp string. I have attempted changing the temp string, but that does not seem to do anything.
I have been watching VS2012's locals window and that seems to be the only thing that isn't working as intended.
Upvotes: 0
Views: 322
Reputation: 2719
Try this:
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));
}
Upvotes: 0
Reputation:
You need to assign the value back to temp:
temp = temp.PadLeft(padCount, '-');
Here is a complete working method:
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;
}
Upvotes: 1