我正在尝试制作简单的应用并陷入困境。用户从日期选择器1 28.01.2015和结束日期选择器2 29.01.2015中选择开始日期,然后通过复选框和按钮标记他/她想要的内容。控制台中的输出应该与此类似,具体取决于标记的内容。
tar zxcvf /.../c/folder/folder/$HOSTNAME.20150128.log;zxcvf /.../c/folder/folder/$HOSTNAME.20150129.log`
但是选择作为开始日期的日期在控制台中返回两次,我不知道出了什么问题:
zxcvf /.../c/folder/folder/$HOSTNAME.20150128.log;zxcvf /.../c/folder/folder/$HOSTNAME.20150128.log;zxcvf /.../c/folder/folder/$HOSTNAME.20150129.log
以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Renci.SshNet;
using System.Configuration;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GlobalMethods configRead = new GlobalMethods();
configRead.ConfigRead();
}
...
private void button1_Click(object sender, EventArgs e)
{
GlobalMethods configRead = new GlobalMethods();
configRead.ConfigRead();
GlobalMethods dateIterator = new GlobalMethods();
dateIterator.Iterator();
GlobalVariables.comminit = null;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
GlobalVariables.cLM = true;
}
else if (!checkBox1.Checked)
{
GlobalVariables.cLM = false;
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
GlobalVariables.dateStart = dateTimePicker1.Value;
}
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
GlobalVariables.dateEnd = dateTimePicker2.Value;
//string endDate = dateTimePicker2.Value.ToString("yyyyMMdd");
}
}
public static class GlobalVariables
{
// Log variables below
public static Boolean mLM;
public static Boolean cLM;
public static Boolean hLM;
public static Boolean pLM;
public static Boolean prLM;
public static Boolean compressMarked = true;
public static String iterate;
public static String comminit;
public static String pH;
public static String pP;
public static String pC;
public static String ppP;
public static String pM;
// Time variables below
public static DateTime dateStart = DateTime.Now;
public static DateTime dateEnd = DateTime.Now;
}
public class Instructions
{
public static String hLl()
{
if (GlobalVariables.compressMarked)
{
return "tar zxcvf " + GlobalVariables.pH + "$HOSTNAME." + GlobalVariables.iterate + ".log;";
}
else { return "wget " + GlobalVariables.pH + "$HOSTNAME." + GlobalVariables.iterate + ".log;"; }
}
...
public static String commandsBath()
{
if (GlobalVariables.mLM == true)
{
GlobalVariables.comminit += mLl();
}
if (GlobalVariables.cLM == true)
{
GlobalVariables.comminit += cLl();
}
if (GlobalVariables.hLM == true)
{
GlobalVariables.comminit += hLl();
}
if (GlobalVariables.pLM == true)
{
GlobalVariables.comminit += pLl();
}
if (GlobalVariables.prLM == true)
{
GlobalVariables.comminit += pLlp();
}
return GlobalVariables.comminit;
}
}
public class GlobalMethods
{
public void Iterator()
{
for (DateTime i = GlobalVariables.dateStart; i <= GlobalVariables.dateEnd; i = i.AddDays(1))
{
string iterated = i.ToString("yyyyMMdd");
GlobalVariables.iterate = iterated;
}
}
public void ConfigRead()
{
...
}
public void ConfigWrite()
{
...
}
}
}
希望有人可以提供帮助。
答案 0 :(得分:0)
没关系,找到了解决方案。而不是使用+ =并添加if语句的结果,最好使用如下所示的String构建器。这样它就可以正常工作并且更简单。
var sb = new StringBuilder();
if (GlobalVariables.mLM == true) sb.Append(mLl());
if (GlobalVariables.cLM == true) sb.Append(cLl());
...
return GlobalVariables.comminit = sb.ToString();