c#获取对话框的所选文件的文件名和路径名

system.window.form.openfiledialog OpenFileDialog;

openFileDialog=new OpenFileDialog();

openFileDialog.Title="打开文件”;

if (openFileDialog.ShowDialog() == DialogResult.OK)
      {
          string file = openFileDialog.FileName;(获取整个文件名)
          string fileOpenPath, fileName;
          int index = file.LastIndexOf("\\");(获取第一个开始以“\\”结尾的位置)

          fileOpenPath =file.Substring(0,index);(从0截取到“\\"的长度)

fileName = file.Substring(index+1,file.Length-index-1);(从index加一到总长度)


}


猜你喜欢

转载自blog.csdn.net/qq_41146515/article/details/80224048