1

我是编程初学者。我的问题与使用日期时间选择器有关。我在我的程序中使用文件夹浏览器对话框来访问文件夹中的多个文件,然后对它们进行操作以得出各种输出,例如平均值、最小值和最大值等。然后我包括数据时间选择器和文件夹浏览器对话框按钮,以便对文件执行任务特定时间段。但是,当代码运行时,它要求我指定文件夹的位置。然后程序生成所需的输出,而不要求我需要输出的时间间隔。在选择包含 csv 文件的文件夹后,如何让程序要求我选择时间间隔。代码如下:

namespace Filereader_m_15062012
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog(); // Show the dialog.
            // create a list to insert the data into
            //put all the files in the root directory into array
            string[] fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.csv");

            // Display all files.
            TextWriter tw1 = new StreamWriter("C:/Users/a3708906/Documents/Filereader m 26062012 -2/Filereader m 22062012/listoffiles.txt");

            List<string> filenames = new List<string>();
            tw1.WriteLine("--- Files: ---");
            foreach (string path in fileEntries)
            {
                tw1.WriteLine(path);
            }

            tw1.Close();

            //int count = 0;
            //float sum = 0;

            string fileID1 =""; 
            List<string> fileID   = new List<string>();
            List<string> means = new List<string>();
            List<string> maximums = new List<string>();
            List<string> minimums = new List<string>();


            TextWriter tw2 = new StreamWriter("C:/Users/a3708906/Documents/Filereader m 26062012 -2/Filereader m 22062012/errorlist.txt");
            foreach (string path in fileEntries)
            {
                if (File.Exists(path))
                {  
                    string text = "";
                    // create a list to insert the data into
                    List<float> noise = new List<float>();
                    List<DateTime> time = new List<DateTime>();

                    TextWriter tw3 = new StreamWriter("C:/Users/a3708906/Documents/Filereader m 26062012 -2/Filereader m 22062012/rawdata.txt");

                    string file = path;

                    FileInfo src = new FileInfo(file);
                    TextReader reader = src.OpenText();
                    text = reader.ReadLine();

                    // while the text being read in from reader.Readline() is not null
                    while (text != null)
                    {
                        text = reader.ReadLine();

                        {
                            while (text != null)
                            {
                                text = reader.ReadLine();
                                if (text != null)
                                {
                                    string[] words = text.Split(',');
                                    noise.Add(Convert.ToSingle(words[3]));
                                    time.Add(Convert.ToDateTime(words[2]));
                                    fileID1 = words[1];

                                    // write text to a file
                                    tw3.WriteLine(text);
                                    //foreach (string word in words)
                                    //{
                                    //    tw.WriteLine(word);
                                    //}
                                }

                            }
                        }

                        tw3.Close();

                        TextWriter tw4 = new StreamWriter("C:/Users/a3708906/Documents/Filereader m 26062012 -2/Filereader m 22062012/noise.txt");

                        //float minValue = noise.Min();
                        float maxValue = Enumerable.Max(noise);
                        float mean = Enumerable.Average(noise); 
                        float minValue = Enumerable.Min(noise);
                        //float maxValue = noise.Max();
                        //float mean = noise.Average(); 

                        //foreach (float ns in noise)
                        {
                            //tw4.WriteLine(Convert.ToString(ns));
                            //count++;
                            //sum += ns;
                            //mean = sum / count; 

                            //float min1 = 0; 
                            //if (ns > max) 
                                //max = ns; 
                            //else if (ns < max) 
                                //min1 = ns; 
                            //if (min1 < min && min1 > 0) 
                            //min = min1; 
                        }

                        tw4.Close();

                        fileID.Add(Convert.ToString(fileID1)); 
                        means.Add(Convert.ToString(mean));
                        maximums.Add(Convert.ToString(maxValue));
                        minimums.Add(Convert.ToString(minValue));
                    }                            
                }

                else
                {
                    tw2.WriteLine("{0}", path);
                }

                tw2.Close();
            }

            TextWriter tw5 = new StreamWriter("C:/Users/a3708906/Documents/Filereader m 26062012 -2/Filereader m 22062012/IDs.txt");
            tw5.WriteLine("FILE IDS");
            tw5.WriteLine("==========");
            foreach (string m in fileID)
            {
                tw5.WriteLine("MOTE {0}", (m));
            }

            tw5.Close();


            TextWriter tw6 = new StreamWriter("C:/Users/a3708906/Documents/Filereader m 26062012 -2/Filereader m 22062012/results.txt");
            tw6.WriteLine("File IDs" + "          " + "Means" + "          " + "Maximums" + "        " +  "Minimums");
            tw6.WriteLine("========" + "          " + "=====" + "          " + "========" + "        " +  "========");
            for (int i=0; i < fileID.Count; i++)
                   {
                       int j = i;
                       tw6.WriteLine("Mote" + " " + Convert.ToString(fileID[i]) + "          " + Convert.ToString(means[j]) + "          " + Convert.ToString(maximums[j]) + "              " + Convert.ToString(minimums[j]));         
                    }

            tw6.Close();

        }       


        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }


        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

    }

我将感谢任何人对此的帮助。谢谢。

4

2 回答 2

2

尝试使用两个日期时间选择器制作另一个表格

form2 应该是这样的

标签说开始

日期时间选择器开始

标签说结束

日期时间选择器结束

按钮确定

它的代码应该是这样的:

 private void btok_Click(object sender, EventArgs e)
    {
        if (dtpickerend.Value == dtpickerstart.Value)
            MessageBox.Show("Please select an interval", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        else
        {
            Program.date_start = dtpickerstart.Value;
            Program.date_end = dtpickerend.Value;
            this.DialogResult = DialogResult.OK
            this.Close();
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
    }

也去 program.cs 并添加这两个变量

/// <summary>
    /// The main entry point for the application.
    /// </summary>
    public static DateTime date_start;
    public static DateTime date_end;
    [STAThread]
    static void Main()

最后在您想要调用该表单时在您的主表单中添加以下内容:

secondform datepicker_dialog = new secondform();
        DialogResult dr;
        dr = datepicker_dialog.ShowDialog();
        if (dr == DialogResult.OK)
            //means that the user succseful selected an interval
            ;
        else
            //means the user closed the second form
            ;
于 2012-06-27T16:40:53.393 回答
1

好吧,您在Load事件处理程序中做所有事情,在加载表单时立即调用它,因此您没有时间设置两个 DateTimePickers。

向表单添加一个按钮,然后将所有内容移至其Click事件处理程序。当然,您必须更改您的代码,以便它考虑到两个 DateTimePickers 中设置的值,您可以通过它们的Value属性访问它们。

目前尚不清楚您需要两个日期来做什么,但如果您只想根据文件的创建/上次修改/上次访问日期来考虑文件,您可以导入System.IO命名空间并使用Directory,FileFileInfo类中的相关方法和属性。

于 2012-06-27T16:57:28.073 回答