首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在winforms应用程序中获取文件路径

在WinForms应用程序中获取文件路径,可以使用以下方法:

  1. 使用OpenFileDialog组件:
代码语言:csharp
复制
using System;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                MessageBox.Show(filePath);
            }
        }
    }
}
  1. 使用SaveFileDialog组件:
代码语言:csharp
复制
using System;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                MessageBox.Show(filePath);
            }
        }
    }
}
  1. 使用FolderBrowserDialog组件:
代码语言:csharp
复制
using System;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string folderPath = folderBrowserDialog.SelectedPath;
                MessageBox.Show(folderPath);
            }
        }
    }
}
  1. 使用FileSystemWatcher组件:
代码语言:csharp
复制
using System;
using System.IO;
using System.Windows.Forms;

namespace WinFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string folderPath = @"C:\example";
            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(folderPath);
            fileSystemWatcher.Created += FileSystemWatcher_Created;
            fileSystemWatcher.EnableRaisingEvents = true;
        }

        private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
        {
            MessageBox.Show($"文件已创建:{e.FullPath}");
        }
    }
}

以上方法都可以在WinForms应用程序中获取文件路径,具体使用哪种方法取决于您的需求。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券