我有這個程式應該讀取同一個檔案夾中的多個文本檔案,在那個檔案夾中有 2 個文本檔案應該被讀取,好的,現在我必須根據該檔案夾中的文本檔案總數生成一個新的 TextBox .
主要目標
在每個文本框中加載這些檔案的內容
- File1.txt 內容將被加載到 TextBox1 中。
- File2.txt 內容將被加載到 TextBox2 中。
File1.txt 的內容:
Title 1
ABCDEFG
File2.txt 的內容:
Title 2
1234567890
問題
將這些檔案的內容加載到每個 TextBox 中可以正常作業,但問題是沒有在 TextBox 上創建換行符。
相反,我在每個文本框上都得到了這個:
文本框 1:
Title 1ABCDEFG
文本框 2:
Title 21234567890
程式加載后立即執行必要的操作:
private void Form1_Load(object sender, EventArgs e) {
flowLayoutPanel1.AutoScroll = true;
var dirs_notes = @"C:\MAIN_LOC\DATA_LOC\";
var count_notes = Directory.GetFiles(dirs_notes,"*.*",SearchOption.AllDirectories).Count();
string setup_path = @"C:\MAIN_LOC\DATA_LOC\";
if(Directory.Exists(setup_path)) {
string[] get_notes = Directory.GetFiles(dirs_notes, "*.txt", SearchOption.AllDirectories);
string[] get_texts = get_notes.Select(x => File.ReadAllText(x)).ToArray();
for(int i=0; i<count_notes; i ) {
int top = 25;
int h_p = 170;
var load_note = new Guna2TextBox() {
Text = "\n" get_texts[i],
Name = "Note" i,
Multiline = true,
AcceptsTab = true,
AcceptsReturn = true,
WordWrap = false,
Width = 230,
Height = 145,
BorderRadius = 8,
Font = new Font("Bahnschrift", 13),
ForeColor = Color.White,
FillColor = ColorTranslator.FromHtml("#1E1E1E"),
BorderColor = ColorTranslator.FromHtml("#2C2C2C"),
Location = new Point(450, top)
};
top = h_p;
flowLayoutPanel1.Controls.Add(load_note);
}
} else {
MessageBox.Show("There's problem with loading notes..", "Flow Notes System");
}
}
uj5u.com熱心網友回復:
修復可能是使用 Lines 屬性而不是 Text 屬性。
var dirs_notes = @"C:\MAIN_LOC\DATA_LOC\";
// Be sure to count only txt files here...
var count_notes = Directory.GetFiles(dirs_notes,"*.txt",SearchOption.AllDirectories).Count();
string setup_path = @"C:\MAIN_LOC\DATA_LOC\";
if(Directory.Exists(setup_path)) {
string[] get_notes = Directory.GetFiles(dirs_notes, "*.txt", SearchOption.AllDirectories);
var get_texts = get_notes.Select(x => File.ReadLines(x));
for(int i=0; i<count_notes; i ) {
....
var load_note = new Guna2TextBox() {
Lines = "\n" get_texts[i].ToArray(),
但更好的方法是改用它:
// No counting here (counting means load all
// files names in a memory array and the get its length
var files = Directory.EnumerateFiles(dirs_notes,"*.txt",SearchOption.AllDirectories)
int i = 1;
// Enumerate files one by one without loading all names in memory
foreach(string file in files) {
int top = 25;
int h_p = 170;
var load_note = new Guna2TextBox() {
// Set the textbox with the current file lines content
// again one by one without loading all texts in memory
Lines = File.ReadLines(file).ToArray(),
Name = "Note" i,
...
}
i ;
.....
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/506809.html
