WinForm下选择文件目录(非文件)

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public class FolderDialog : FolderNameEditor
    {
        FolderNameEditor.FolderBrowser fDialog = new
        System.Windows.Forms.Design.FolderNameEditor.FolderBrowser();
        public FolderDialog()
        {
        }
        public DialogResult DisplayDialog()
        {
            return DisplayDialog("请选择一个文件夹");
        }

        public DialogResult DisplayDialog(string description)
        {
            fDialog.Description = description;
            return fDialog.ShowDialog();
        }
        public string Path
        {
            get
            {
                return fDialog.DirectoryPath;
            }
        }
        ~FolderDialog()
        {
            fDialog.Dispose();
        }
    }
}

 

调用方式:

        private void btn_SelSrcDir_Click(object sender, EventArgs e)
        {
            //FolderBrowserDialog folder = new FolderBrowserDialog();
            //txt_PcSrc.Text = folder.RootFolder.ToString();
            FolderDialog folder = new FolderDialog();
            folder.DisplayDialog();
            txt_PcSrc.Text = folder.Path;
        }

转载于:https://www.cnblogs.com/Deckard/archive/2009/05/15/1457296.html

要将 WinForm 文件选择选择文件上传到指定目录下,您需要使用 `System.IO.File` 类的 `Copy()` 方法。 以下是一个示例代码片段,展示如何将选择文件上传到指定目录下: ```C# string sourceFilePath = openFileDialog1.FileName; // 获取选择文件的完整路径 string targetDirectory = "C:\\Uploads"; // 指定目标目录的路径 try { // 将选择文件复制到目标目录下 System.IO.File.Copy(sourceFilePath, System.IO.Path.Combine(targetDirectory, System.IO.Path.GetFileName(sourceFilePath)), true); MessageBox.Show("文件上传成功!"); } catch (Exception ex) { // 处理异常 MessageBox.Show("文件上传失败:" + ex.Message); } ``` 在此示例中,我们首先获取选择文件的完整路径。然后,将目标目录的路径存储在变量 `targetDirectory` 中。 在 `try` 块中,我们使用 `System.IO.File.Copy()` 方法将选择文件复制到目标目录中。`Copy()` 方法的第一个参数是源文件的完整路径,第二个参数是目标文件的完整路径,第三个参数是一个布尔值,指示是否覆盖现有的目标文件。我们使用 `System.IO.Path.GetFileName()` 方法获取源文件文件名,并使用 `System.IO.Path.Combine()` 方法将目标目录和文件名组合成目标文件的完整路径。 如果文件上传成功,将会显示一个消息框。如果无法上传文件,将会抛出异常。在此示例中,我们在 `catch` 块中处理异常并向用户显示错误消息。 请注意,要使用 `System.IO.File` 类上传文件,您需要确保目标目录存在并且您有足够的权限将文件复制到该目录中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值