0

textbox何かを取得/保存するために使用されるようなプログラムを何度も見てきましたpath...ボタンがあり、それをクリックすると、ディレクトリを選択するように求めるプロンプトが表示されます。 ? どうすればそれができますか?を読むfile.txt必要があり、アプリケーションでこれを開く必要がありfile.txtます。この「プロンプト」を開く方法は? 次に、同じ方法で a を保存する必要がありdestination pathます...実際には atextboxまたは何か他のものですか?

ありがとう

4

3 に答える 3

2

テキストボックスの横にボタンを作成する必要があります。

ボタンの Click イベント ハンドラーで、 を作成して表示しSaveFileDialog、その結果をテキスト ボックスのテキストに割り当てます。

于 2013-01-29T17:08:12.557 に答える
2

をフォームに追加する必要がありOpenFileDialogます ( MSDN に詳細があります) 。

このサンプルは、私ができるよりもよく説明する必要があります!

private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            using (myStream)
            {
                // Insert code to read the stream here.
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }
}
}
于 2013-01-29T17:09:36.673 に答える
1

OpenFileDialog を使用できます

string path;
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
    path = file.FileName;
}

これで、ファイル パスが文字列に保存され、ファイルを操作できるようになります。

于 2013-01-29T17:10:04.210 に答える