string path=openFileDialog.FileName-"abc.txt";

解决方案 »

  1.   

    先用index方法找到最后一个"\"在哪里,然后用substring取出前面的路径就好了
      

  2.   

    string name=@"c:\dext\aaa\aaaa\d.txt";
    int lon=name.LastIndexOf("\\");
    name=name.Substring(1,lon-1);
    MessageBox.Show(name);
      

  3.   

    string path = openFileDialog.FileName;
    path = path.SubStr(0, path.LastIndexOf("\\");
      

  4.   

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.InitialDirectory = Environment.CurrentDirectory;
    ofd.Filter = "txt files (*.ini)|*.ini|All files (*.*)|*.*";
    ofd.FilterIndex = 1;
    ofd.Title = "Open Calling Table File (.ini):";
    ofd.RestoreDirectory = false;
    if(ofd.ShowDialog() == DialogResult.OK)
    {
    // Show in TextBox
    this.txtFilePath.Text = ofd.FileName;
    // Show in ListBox
    this.lbCallTable.BeginUpdate();
    string buffer;
    StreamReader myFile = File.OpenText(this.txtFilePath.Text);
    while ((buffer = myFile.ReadLine()) != null)
    {
    this.lbCallTable.Items.Add(buffer);
    }
    this.lbCallTable.EndUpdate();
    }
      

  5.   

    我想楼主的真实意图是想有一个能够获取“路径”的对话框,而 .NET 提供的 OpenFileDialog 只能用来获取“文件”。楼上众位已经提供了从文件名中解析路径名称的方法,这么做从理论上讲没有任何问题。然而,如果用到实际应用程序中,那么你从一个菜鸟用户的角度想一想,用户到底要做什么?其实只是要他选一个路径而已,而非要去选一个文件吗?可惜的是 .NET 没有提供文件夹选择对话框,只能自己通过 Shell API 调用或其他有效的方法实现一个了。具体的实现不是三言二语说得清楚的,请参阅:
    http://www.codeproject.com/cs/miscctrl/folderbrowser.asp
      

  6.   

    .Net Framework 1.1 中是有文件夹选择对话框的