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

使用できる可能性がいくつかあります。1 つは、ユーザーが最初にパスを入力したときに -file
を作成することです。XMLファイルの存在を確認し、存在する場合はそこから読み取り、存在しない場合は作成してデータを書き込みます。もちろん、XML ファイルを編集することもできます。

System.Xml.XmlDocument-クラスがあります

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 に答える