2014-02-19 48 views
2

嗨,我想用對話框的形式選擇文本文件,而不必使用給定的路徑。我將如何做到這一點?將opentext更改爲opendialog

我想用opendialog替換opentext嗎?我試過,但我得到的流我想使用的StreamReader錯誤....

private void button2_Click(object sender, EventArgs e) 
    { 

     using (StreamReader reader = File.OpenText("c:\\myparts.txt")) 
     { 
      label3.Text = "Ready to Insert"; 
      textBox7.Text = reader.ReadLine(); 
      textBox8.Text = reader.ReadLine(); 
      textBox9.Text = reader.ReadLine(); 
      textBox10.Text = reader.ReadLine(); 
} 
+0

Opendialog返回所選擇的文件路徑,而不是一個流,你需要使用它作爲參數來打開文本。 – BlackICE

回答

2

我想opendialog替換OpenText公司?我試過,但我得到 錯誤與流我想使用的StreamReader ....

解決方案1:您可以指定返回Stream通過openFileDialog.OpenFile()StreamReader

試試這個:

 if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      using (var reader = new StreamReader(openFileDialog1.OpenFile())) 
      { 
       label3.Text = "Ready to Insert"; 
       textBox7.Text = reader.ReadLine(); 
       textBox8.Text = reader.ReadLine(); 
       textBox9.Text = reader.ReadLine(); 
       textBox10.Text = reader.ReadLine(); 
      } 
     } 

解決方案2:您可以直接分配openFileDialog().FileName財產 Path個參數File.OpenText()方法如下:

 if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      using (var reader = new StreamReader(openFileDialog1.OpenText(openFileDialog1.FileName))) 
      { 
       label3.Text = "Ready to Insert"; 
       textBox7.Text = reader.ReadLine(); 
       textBox8.Text = reader.ReadLine(); 
       textBox9.Text = reader.ReadLine(); 
       textBox10.Text = reader.ReadLine(); 
      } 
     } 

解決方案3:

int startCount=7; 
int endCount=10; 
string preText="textBox"; 
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    String fileName=openFileDialog1.FileName; 
    foreach(var line in File.ReadLines(fileName)) 
    { 
    ((TextBox) (this.Controls.Find(preText+startCount,true)[0])).Text=line; 
    if(startCount==endCount) 
     break; 

     startCount++; 
    } 
} 

注意:如果你想文件內容分配到多個textbox'es

試試這個 1:所有的TextBoxControls應該以preText的值開始。
注2:在上述解決方案中,您可以按照您的要求更改startCountendCount

例如,如果你想文件contenet分配給開始從textBox3textBox23你需要改變在如下上述代碼參數20個TextBox控件:

preText="textBox"; 
startCount = 3; 
endCount = 23; 
+0

真棒謝謝你。 –

+0

@ user3324892:歡迎您:)我很樂意幫助您。 –

4

你要這樣呢?

OpenFileDialog dlg = new OpenFileDialog(); 

if (dlg.ShowDialog() == DialogResult.OK) 
{ 
    using (var reader = File.OpenText(dlg.FileName)) 
    { 
     ... 
    } 
} 
2
private void button2_Click(object sender, EventArgs e) 
{ 
     string fileToOpen = ""; 

     OpenFileDialog dialog = new OpenFileDialog(); 
     dialog.Title = "Browse for file..."; 
     dialog.RestoreDirectory = true; 

     DialogResult result = dialog.ShowDialog(); 

     if (result == DialogResult.OK) 
     { 
      fileToOpen = dialog.FileName; 
     } 

     using (StreamReader reader = File.OpenText(fileToOpen)) 
     { 
      label3.Text = "Ready to Insert"; 
      textBox7.Text = reader.ReadLine(); 
      textBox8.Text = reader.ReadLine(); 
      textBox9.Text = reader.ReadLine(); 
      textBox10.Text = reader.ReadLine(); 
     } 
} 
+0

如果這是你的解決方案。請隨時「檢查」這個答案。 ;) 很高興我能幫上忙。 –