0
private void button1_Click(object sender, EventArgs e)
    {


        OpenFileDialog newOpen = new OpenFileDialog();

       DialogResult result = newOpen.ShowDialog();

       this.textBox1.Text = result + "";

    }

「OK」を返すだけです

私は何を間違っていますか?ファイルへの PATH を取得し、テキスト ボックスに表示したいと考えています。

4

4 に答える 4

1

ファイル名にアクセスする必要があります。

 string filename = newOpen.FileName;

またはファイル名 (複数のファイル選択を許可している場合):

newOpen.FileNames;

参考:OpenFileDialogクラス

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. Error: " + ex.Message);
        }
    } 
}
于 2013-10-14T23:41:26.397 に答える
1

このShowDialogメソッドは、ユーザーが または を押したかどうかを返しOKますCancel。これは有用な情報ですが、実際のファイル名はダイアログのプロパティとして保存されます

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog newOpen = new OpenFileDialog();
        DialogResult result = newOpen.ShowDialog();
        if(result == DialogResult.OK) {
              this.textBox1.Text = newOpen.FileName;
        }
    }
于 2013-10-14T23:43:03.433 に答える
0

既存のファイルをデフォルトとして使用し、新しいファイルを取得する例を次に示します。

private string open(string oldFile)
    {
        OpenFileDialog newOpen = new OpenFileDialog();
        if (!string.IsNullOrEmpty(oldFile))
        {
          newOpen.InitialDirectory = Path.GetDirectoryName(oldFile);
          newOpen.FileName = Path.GetFileName(oldFile);
        }

        newOpen.Filter = "eXtensible Markup Language File  (*.xml) |*.xml"; //Optional filter
        DialogResult result = newOpen.ShowDialog();
        if(result == DialogResult.OK) {
              return newOpen.FileName;
        }

        return string.Empty;
    }

Path.GetDirectoryName(file) : リターン パス

Path.GetFileName(file) : ファイル名を返す

于 2013-10-14T23:55:31.230 に答える
0

インスタンスのFileNameプロパティを読み取る必要があります。OpenFileDialogこれにより、選択したファイルのパスが取得されます。

于 2013-10-14T23:41:36.203 に答える