I created a program that monitors what happens to the files inside a directory. For example if I add a new file, it shows the path and the name of the new file. Though I coded it in Windows Forms, I want to change it to a Console Application so that it shows the results on the console
My question is that how can I browse a folder using a console? any ideas thanks in advance
The Windows Forms code is below:
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);
}
}
this is my whole code
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)));
}
}
}
}
First, you need to add reference to System.Windows.Forms
Second, add STAThread attribute to your method.
For example:
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);
}
}
}