2

感谢大家到目前为止的帮助!我对 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();
}
4

2 回答 2

0

您可以使用此处描述的“用户设置”:http: //msdn.microsoft.com/en-us/library/bb397750.aspx

//编辑:

假设您有一个名为“MySetting”的字符串类型的用户设置

如果你想阅读它:

var someVar = Properties.Settings.Default.MySetting;

如果你想写它(假设你的数据在someVar):

Properties.Settings.Default.MySetting = someVar;
Properties.Settings.Default.Save();

对 Save() 的调用会保留您的更改...更改已绑定到 Windows 用户帐户

在设计时,您的设置应定义为 Scope USER

于 2013-04-08T18:56:22.740 回答
0

您可以使用几种可能性。一件事是在用户第一次输入路径时
创建一个-file。XML您可以检查文件是否存在,如果存在,则从中读取,如果不存在,则创建它并向其写入数据。当然,您可以编辑您的 XML 文件。

System.Xml.XmlDocument-class

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    string inputpath = "C:\....";

    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Create a new node and add it to the document.
    //The text node is the content of the price element.
    XmlElement elem = doc.CreateElement("Inputpath");
    XmlText text = doc.CreateTextNode(inputpath);
    doc.DocumentElement.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(text);

    doc.Save(Console.Out);

  }
}

请参阅此处以获取参考。

您也可以将值写入注册表。就像一种可能性一样。

于 2013-04-08T18:58:24.270 回答