C# get file/folder path

Get folder path

            //选择文件夹路径
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            //提示信息
            dialog.Description = "请选择文件路径";
            string Path = "";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Path = dialog.SelectedPath + @"\标本复查表.xlsx";
            }

The final result is the string Path of the absolute path of the folder, which can be spliced ​​for other purposes.

Get file path

            //选择文件路径
            var ofd = new OpenFileDialog();
            string path = string.Empty;
            //筛选文件类型
            ofd.Filter = "excel|*.xlsx";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path = ofd.FileName;
            }

The same as obtaining the file path, the path obtained is the absolute path string containing the file suffix. Filter is used to filter the file type. For details, please refer to the following article.

Filter property setting of openFileDialog

File save dialog

            string path = string.Empty;
            SaveFileDialog sfd = new SaveFileDialog();
            //默认显示名称
            sfd.FileName = "标本复查表.xlsx";
            //设置匹配文件格式,可包含多种
            sfd.Filter = "Excel(*.xlsx)|*.xlsx";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
            else
            {
                return;
            }

In the save dialog box, the path finally got contains the full file name absolute path string, which can cooperate with the I/O operation file, but it can directly edit the file name in the dialog box. If there is a name conflict in the file, it will prompt whether to overwrite Dialog box. And you can customize the prompt dialog box directly to delete and other operations, the code is given below.

 string path = string.Empty;
            SaveFileDialog sfd = new SaveFileDialog();
            //取消自带提示框提示
            sfd.OverwritePrompt = false;

            sfd.FileName = "标本复查表.xlsx";
            sfd.Filter = "Excel(*.xlsx)|*.xlsx";

            //自定义提示框,点击保存弹出框的打开或者保存按钮触发
            sfd.FileOk += (s, e) =>
            {
                if (File.Exists(sfd.FileName))
                {
                    DialogResult ds = MessageBox.Show("文件已经存在,是否删除文件?", "提        
                       示", MessageBoxButtons.OKCancel);
                    if (ds == DialogResult.OK)
                    {
                        File.Delete(sfd.FileName);
                    }
                }
            };

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                path = sfd.FileName;
            }
            else
            {
                return;
            }

When you select a file or enter a file in the folder, the above customized prompt box will pop up. Click OK to delete the file and retain the absolute path of the deleted file, and then create a file based on the absolute path. It is a stupid way to rewrite the file. The article is just my own essay. If there are any mistakes or improprieties, I hope you can correct me.

Guess you like

Origin blog.csdn.net/qq_40702130/article/details/103574817