在WinForms应用程序中获取文件路径,可以使用以下方法:
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);
}
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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应用程序中获取文件路径,具体使用哪种方法取决于您的需求。
领取专属 10元无门槛券
手把手带您无忧上云