**1875 Kovezes
1875 Koszenbanya
1875 Lelenc
1875 Laktanyak
1875 Dologhaz .. ...
1876 Honvedseg
1876 Hidak
**
I have this filestructure in txt-file
"Description: First part of the line is 4 digits-and it can be equal (and root foldername) and It's have to contain only the same 4 digits subfolder. the txt-file is very long approximately 18000 lines
Example:
C:/DATA/1875/Kovezes C:/DATA/1875/Koszenbanya
private void button3_Click(object sender, EventArgs e)
{
string line;
string linecopy;
int linecopyc;
//string root = @"C:\Temp";
// string subdir = @"~/ASD/";
var file = new System.IO.StreamReader("adatforras.txt");
while ((line = file.ReadLine()) != null)
{
listBox1.Items.Add(line);
//linecopy = line;
linecopy = line.Substring(0,4);
label4.Text = linecopy;
linecopyc = line.Length - 4;
label4.Text = line.Substring(4, linecopyc);
if (!Directory.Exists(linecopy))
{
di = Directory.CreateDirectory(linecopy);
DirectoryInfo dis = di.CreateSubdirectory(label4.Text);
}
else
{
DirectoryInfo dis = di.CreateSubdirectory(label4.Text);
}
}
}
Question is: what is the main problem with this code?
I think your substring begins with a space:
label4.Text = line.Substring(4, linecopyc);
Try trimming it or start at position 5
label4.Text = line.Substring(5, linecopyc);
C# supports being able to create all directories in the path on the way to the path you want, so you can do something like this:
while ((line = file.ReadLine()) != null) {
// Other parts with labels etc.
var directoryParts = line.split(" "); // To get number and name separately
System.IO.Directory.CreateDirectory(Path.Combine("C:\\Data", directoryParts[0], directoryParts[1]));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.