2

也许我有一个更好的主意。这是原始代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
    private bool pause = false;
    private bool cut1 = false;
    private bool copy1 = false;
    Form2 popup = new Form2();

  
    public Form1()

    {
        InitializeComponent();
    }
    // The lines with performed actions of a file
    private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }
    private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }
    
    //1st directory
    private void button2_Click(object sender, EventArgs e)
    {
        if (dlgOpenDir.ShowDialog() == DialogResult.OK)
        {
            fileSystemWatcher1.EnableRaisingEvents = false;  // Stop watching
            fileSystemWatcher1.IncludeSubdirectories = true;
            fileSystemWatcher1.Path = dlgOpenDir.SelectedPath;
            textBox1.Text = dlgOpenDir.SelectedPath;         // Text of textBox2 = Path of fileSystemWatcher2
            fileSystemWatcher1.EnableRaisingEvents = true;   // Begin watching
        }
    }
    //2nd directory
    private void button3_Click(object sender, EventArgs e)
    {
        if (dlgOpenDir.ShowDialog() == DialogResult.OK)
        {
            fileSystemWatcher2.EnableRaisingEvents = false;  // Stop watching
            fileSystemWatcher2.IncludeSubdirectories = true;
            fileSystemWatcher2.Path = dlgOpenDir.SelectedPath;
            textBox2.Text = dlgOpenDir.SelectedPath;         // Text of textBox2 = Path of fileSystemWatcher2
            fileSystemWatcher2.EnableRaisingEvents = true;   // Begin watching
        }
    }
    //log
    private void button1_Click(object sender, EventArgs e)
    {

        DialogResult resDialog = dlgSaveFile.ShowDialog();
        if (resDialog.ToString() == "OK")
        {
            FileInfo fi = new FileInfo(dlgSaveFile.FileName);
            StreamWriter sw = fi.CreateText();
            foreach (string sItem in listBox1.Items)
            {
                sw.WriteLine(sItem);
            }
            sw.Close();
        }
    }
    //pause watching
    private void pause_button_Click(object sender, EventArgs e)
    {
        if (!pause)
        {
            pause = true;
            pause_button.Text = "Unpause";
        }
        else
        {
            pause = false;
            pause_button.Text = "Pause Watching";
        }
    }
    //clear listbox
    private void clear_button_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear(); 
    }

    private void Transfer_Click(object sender, EventArgs e)
    {

        //copy a file
        if (copy1)
        {
            DialogResult dialogresult = popup.ShowDialog();
            var source = FileBrowseBox.Text;
            var target = Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(source)));

            if (File.Exists(target))
            {

                if (dialogresult == DialogResult.OK)
                {
                    File.Delete(target);
                }
                else if (dialogresult == DialogResult.Cancel)
                {

                }
            }
            else
            {
                File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));
            }
            if (dialogresult == DialogResult.Cancel)
            {

            }
            else
            {
                File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));
            }
        }
        //cut a file
        if (cut1)
        {
            DialogResult dialogresult = popup.ShowDialog();
            var source = FileBrowseBox.Text;
            var target = Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(source)));

            if (File.Exists(target))
            {

                if (dialogresult == DialogResult.OK)
                {
                    File.Delete(target);
                }
                else if (dialogresult == DialogResult.Cancel)
                {

                }
            }
            else
            {
                File.Move(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));
            }
            if (dialogresult == DialogResult.Cancel)
            {

            }
            else
            {
                File.Move(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));
            }
        }
    }
    private void Browse_file_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = openFileDialog1.ShowDialog();
        if (resDialog == DialogResult.OK)
        {
            FileBrowseBox.Text = openFileDialog1.FileName;
        }
    }

    private void Browse_destination_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = folderBrowserDialog1.ShowDialog();
        if (resDialog == DialogResult.OK)
        {
            DestinationBox.Text = folderBrowserDialog1.SelectedPath;
        }
    }

    private void CopyButton_CheckedChanged(object sender, EventArgs e)
    {
        copy1 = true;
    }

    private void Cut_CheckedChanged(object sender, EventArgs e)
    {
        cut1 = true;
    }
}
}

我的应用程序

我希望 Filewatcher 观察 FileBrowseBox 的路径,但使用 FileBrowseBox 你必须选择一个文件。我如何查看文件所在的路径?

4

1 回答 1

0

copy1 和 cut1 是否可能都是真的?在您的代码中,无论复选框的状态如何,您都将变量设置为 true。

尝试将 CheckedChanged 方法更改为

private void CopyButton_CheckedChanged(object sender, EventArgs e)
{
    if (chkCopy.Checked)
    {
        copy1 = true;
        cut1 = false;
        // ToDo: Uncheck checkbox cut1
    }
    else
    {
        copy1 = false;
    {
}

private void Cut_CheckedChanged(object sender, EventArgs e)
{
    if (chkCut.Checked)
    {
        cut1 = true;
        copy1 = false;
        // ToDo: Uncheck checkbox copy1
    }
    else
    {
        cut1 = false;
    {
}

也许使用单选按钮而不是复选框更有趣?当在同一组中时,只能选中一个,而在您的情况下,可以选中两个复选框。这是没有意义的,因为你只能复制或剪切,不能同时复制。

于 2013-06-11T09:02:03.280 回答