感谢大家到目前为止的帮助!我对 c# 和一般代码非常陌生。我有一个问题,我似乎无法找到答案。
我刚刚编写了一个简单的程序,将文件从一个文件夹移动到一个名为当天日期的新文件夹。请看下面:
private void button1_Click(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
string date = (now.ToString("D"));
string a = @"m:\\staff docs\\faxes\\";
string b = @a + date + "\\";
System.IO.Directory.CreateDirectory(b);
DirectoryInfo dir1 = new DirectoryInfo("c:\\blah");
DirectoryInfo dir2 = new DirectoryInfo("@b");
FileInfo[] DispatchFiles = dir1.GetFiles();
if (DispatchFiles.Length > 0)
{
foreach (FileInfo aFile in DispatchFiles)
{
string files = @b + aFile.Name;
int count = 0;
Find :
if (File.Exists(files))
{
files = files + "(" + count.ToString() + ").txt";
count++;
goto Find;
}
aFile.MoveTo(files);
}
}
{
MessageBox.Show("Your files have been moved!");
我想让用户定义源文件夹变量和目标文件夹变量,方法是让他们导航到文件浏览器中的文件夹或 Console.ReadLine - 但不是每次他们运行程序时,只是第一次. 如果他们以后也想改变路径,那将是理想的。
非常感谢!
编辑
我的解决方案是我的表单上的一个按钮,它调用这个块:
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Select source folder";
fbd.ShowDialog();
string Source = fbd.SelectedPath;
Properties.Settings.Default.source = Source;
Properties.Settings.Default.Save();
FolderBrowserDialog fbd2 = new FolderBrowserDialog();
fbd2.Description = "Select destination folder";
fbd2.ShowDialog();
string d1 = fbd2.SelectedPath;
string d2 = "\\";
string Destination = d1 + d2;
Properties.Settings.Default.destination = Destination;
Properties.Settings.Default.Save();
}