C# 如何在console应用程序中使用“浏览文件夹”对话框
我创建了一个程序来监视目录中的文件发生了什么。例如,如果我添加一个新文件,它会显示新文件的路径和名称。虽然我在Windows窗体中对其进行了编码,但我希望将其更改为控制台应用程序,以便在控制台上显示结果 我的问题是如何使用控制台浏览文件夹? 有什么想法吗?谢谢 Windows窗体代码如下所示:C# 如何在console应用程序中使用“浏览文件夹”对话框,c#,console-application,folderbrowserdialog,C#,Console Application,Folderbrowserdialog,我创建了一个程序来监视目录中的文件发生了什么。例如,如果我添加一个新文件,它会显示新文件的路径和名称。虽然我在Windows窗体中对其进行了编码,但我希望将其更改为控制台应用程序,以便在控制台上显示结果 我的问题是如何使用控制台浏览文件夹? 有什么想法吗?谢谢 Windows窗体代码如下所示: private void button1_Click(object sender, EventArgs e) { DialogResult result = fo
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
button1.Enabled = false;
button2.Enabled = true;
directoryPath = folderBrowserDialog1.SelectedPath;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (Directory.Exists(directoryPath))
{
textbox_append("monitor opened");
filesList = Directory.GetFiles(directoryPath);
timer1.Start();
}
else
{
MessageBox.Show("folder does not exist.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error." + ex.Message);
}
}
这是我的全部密码
namespace emin_lab2_Csharp
{
public partial class Form1 : Form
{
public string directoryPath;
string[] filesList, filesListTmp;
IFileOperation[] opList = { new FileProcByExt("jpeg"),
new FileProcByExt("jpg"),
new FileProcByExt("doc"),
new FileProcByExt("pdf"),
new FileProcByExt("djvu"),
new FileProcNameAfter20()
};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
button1.Enabled = false;
button2.Enabled = true;
directoryPath = folderBrowserDialog1.SelectedPath;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (Directory.Exists(directoryPath))
{
textbox_append("monitor opened");
filesList = Directory.GetFiles(directoryPath);
timer1.Start();
}
else
{
MessageBox.Show("Такой папки нету.");
}
}
catch (Exception ex)
{
MessageBox.Show("Error." + ex.Message);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
filesListTmp = Directory.GetFiles(directoryPath);
foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList))
{
textbox_append(elem);
foreach (var op in opList)
{
if (op.Accept(elem)) { op.Process(elem); textbox_append(elem + " - action is performed on the file"); }
}
}
filesList = filesListTmp;
}
public void textbox_append(string stroka)
{
textBox1.AppendText(stroka);
textBox1.AppendText(Environment.NewLine);
}
interface IFileOperation
{
bool Accept(string fileName);
void Process(string fileName);
}
class FileProcByExt : IFileOperation
{
string extName;
string folderName;
public FileProcByExt(string ext = "")
{
extName = ext;
folderName = extName.ToUpper();
}
public bool Accept(string fileName)
{
bool res = false;
if (Path.GetExtension(fileName) == "." + extName) res = true;
return res;
}
public void Process(string fileName)
{
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(fileName),
folderName));
File.Move(fileName,
Path.Combine(Path.GetDirectoryName(fileName),
folderName,
Path.GetFileName(fileName)));
}
}
class FileProcNameAfter20 : IFileOperation
{
public bool Accept(string fileName)
{
return Path.GetFileNameWithoutExtension(fileName).Length > 20;
}
public void Process(string fileName)
{
int cnt = Path.GetFileNameWithoutExtension(fileName).Length;
File.Copy(fileName,
Path.Combine(Path.GetDirectoryName(fileName),
"longname_" + cnt + Path.GetExtension(fileName)));
}
}
}
}
首先,您需要添加对System.Windows.Forms的引用 其次,将属性添加到方法中 例如:
using System;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
Console.Write(ofd.FileName);
}
}
}
首先,您需要添加对System.Windows.Forms的引用 其次,将属性添加到方法中 例如:
using System;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
Console.Write(ofd.FileName);
}
}
}
可能重复浏览可能重复浏览但我无法选择文件夹仅打开文件夹浏览但我无法选择文件夹仅打开文件夹