복사 백업 압축 - C#
프로그램/c# windows form 2009. 2. 25. 17:44 |using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 폴더_자동복사
{
/// <summary>
/// Form1에 대한 요약 설명입니다.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtCopy;
private System.Windows.Forms.TextBox txtOriginate;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lblMsg;
private System.Windows.Forms.Button btnSelect;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnServerCmbCopy;
private System.Windows.Forms.Button btnServerAllCopy;
string sOriginateName = "";
string sCopyName = "";
private System.Windows.Forms.ComboBox cmbPath;
private System.Windows.Forms.ProgressBar pBar1;
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 사용 중인 모든 리소스를 정리합니다.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
/// </summary>
private void InitializeComponent()
{
this.btnSelect = new System.Windows.Forms.Button();
this.txtCopy = new System.Windows.Forms.TextBox();
this.txtOriginate = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.lblMsg = new System.Windows.Forms.Label();
this.cmbPath = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.btnServerCmbCopy = new System.Windows.Forms.Button();
this.btnServerAllCopy = new System.Windows.Forms.Button();
this.pBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// btnSelect
//
this.btnSelect.Location = new System.Drawing.Point(480, 7);
this.btnSelect.Name = "btnSelect";
this.btnSelect.Size = new System.Drawing.Size(104, 55);
this.btnSelect.TabIndex = 0;
this.btnSelect.Text = "선택백업";
this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
//
// txtCopy
//
this.txtCopy.Location = new System.Drawing.Point(48, 40);
this.txtCopy.Name = "txtCopy";
this.txtCopy.Size = new System.Drawing.Size(420, 21);
this.txtCopy.TabIndex = 2;
this.txtCopy.Text = "";
//
// txtOriginate
//
this.txtOriginate.Location = new System.Drawing.Point(48, 8);
this.txtOriginate.Name = "txtOriginate";
this.txtOriginate.Size = new System.Drawing.Size(420, 21);
this.txtOriginate.TabIndex = 1;
this.txtOriginate.Text = "D:\\WebSource\\TestWebRoot\\DirectAngelSolution\\DirectAngel\\bin";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 3;
this.label1.Text = "원본";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 45);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 23);
this.label2.TabIndex = 4;
this.label2.Text = "복사";
//
// lblMsg
//
this.lblMsg.Font = new System.Drawing.Font("굴림", 10F);
this.lblMsg.Location = new System.Drawing.Point(16, 180);
this.lblMsg.Name = "lblMsg";
this.lblMsg.Size = new System.Drawing.Size(568, 80);
this.lblMsg.TabIndex = 5;
this.lblMsg.Text = "<처리결과>";
//
// cmbPath
//
this.cmbPath.Items.AddRange(new object[] {
"D:\\WebSource\\TestWebRoot\\TyLifeSolution\\TyLifeWeb\\bin",
"D:\\WebSource\\TestWebRoot\\DirectAngelSolution\\DirectAngel\\bin",
"D:\\WebSource\\TestWebRoot\\DirectAngelSolution\\Miclub\\bin",
"D:\\WebSource\\TestWebRoot\\DirectAngelSolution\\HanaFos\\bin",
"D:\\WebSource\\WebRoot\\TyLifeSolution\\TyLifeWeb\\bin",
"D:\\WebSource\\WebRoot\\DirectAngelSolution\\DirectAngel\\bin",
"D:\\WebSource\\WebRoot\\DirectAngelSolution\\Miclub\\bin",
"D:\\WebSource\\WebRoot\\DirectAngelSolution\\HanaFos\\bin"});
this.cmbPath.Location = new System.Drawing.Point(48, 104);
this.cmbPath.Name = "cmbPath";
this.cmbPath.Size = new System.Drawing.Size(420, 20);
this.cmbPath.TabIndex = 7;
this.cmbPath.Text = "선택";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 108);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(40, 23);
this.label3.TabIndex = 8;
this.label3.Text = "실섭";
//
// btnServerCmbCopy
//
this.btnServerCmbCopy.Location = new System.Drawing.Point(480, 104);
this.btnServerCmbCopy.Name = "btnServerCmbCopy";
this.btnServerCmbCopy.Size = new System.Drawing.Size(104, 23);
this.btnServerCmbCopy.TabIndex = 9;
this.btnServerCmbCopy.Text = "콤보선택백업";
this.btnServerCmbCopy.Click += new System.EventHandler(this.btnServerCmbCopy_Click);
//
// btnServerAllCopy
//
this.btnServerAllCopy.Location = new System.Drawing.Point(480, 136);
this.btnServerAllCopy.Name = "btnServerAllCopy";
this.btnServerAllCopy.Size = new System.Drawing.Size(104, 23);
this.btnServerAllCopy.TabIndex = 10;
this.btnServerAllCopy.Text = "전체백업";
this.btnServerAllCopy.Click += new System.EventHandler(this.btnServerAllCopy_Click);
//
// pBar1
//
this.pBar1.Location = new System.Drawing.Point(48, 136);
this.pBar1.Name = "pBar1";
this.pBar1.Size = new System.Drawing.Size(420, 23);
this.pBar1.TabIndex = 12;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pBar1,
this.btnServerAllCopy,
this.btnServerCmbCopy,
this.label3,
this.cmbPath,
this.lblMsg,
this.label2,
this.label1,
this.txtCopy,
this.txtOriginate,
this.btnSelect});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private bool SetFileCopy(string sOriginateName, string sCopyName)
{
bool bFlag = true;
if(Directory.Exists(sOriginateName))
{
int iFlag = 1;
string sTempName = sCopyName;
while(Directory.Exists(sCopyName))
{
sCopyName = sTempName + "_" + iFlag.ToString();;
iFlag++;
}
Directory.CreateDirectory(sCopyName);
try
{
string sFileName = "";
string[] files = Directory.GetFiles(sOriginateName);
for(int i=0; i<files.Length; i++)
{
sFileName = Path.GetFileName(files[i]);
File.Copy(files[i], sCopyName + "\\" + sFileName, false);
}
lblMsg.Text = "<처리결과>\n\n디렉토리 복사 완료";
}
catch(Exception ex)
{
lblMsg.Text = "<처리결과>\n\n" + ex.Message;
bFlag = false;
}
}
else
{
//원본디렉토리미존재
lblMsg.Text = "<처리결과>\n\n원본 디렉토리 미존재";
bFlag = false;
}
return bFlag;
}
private void SetProgressBar(int iCnt)
{
pBar1.Visible = true;
pBar1.Minimum = 1;
pBar1.Maximum = iCnt;
pBar1.Value = 1;
pBar1.Step = 1;
}
private void btnSelect_Click(object sender, System.EventArgs e)
{
sOriginateName = txtOriginate.Text;
sCopyName = "";
if(txtCopy.Text.Trim() == "")
sCopyName = sOriginateName + "\\" + DateTime.Now.ToString("yyMMdd_HHmm");
else
sCopyName = txtCopy.Text;
SetProgressBar(2);
for(int i=0; i<1; i++)
{
if(!SetFileCopy(sOriginateName, sCopyName))
break;
else
{
pBar1.PerformStep();
}
}
}
private void btnServerCmbCopy_Click(object sender, System.EventArgs e)
{
if(cmbPath.Text != "선택")
{
sOriginateName = cmbPath.Text;
sCopyName = sOriginateName + "\\" + DateTime.Now.ToString("yyMMdd_HHmm");
SetProgressBar(2);
for(int i=0; i<1; i++)
{
if(!SetFileCopy(sOriginateName, sCopyName))
break;
else
{
pBar1.PerformStep();
}
}
}
else
{
lblMsg.Text = "<처리결과>\n\n선택 하세요";
}
}
private void btnServerAllCopy_Click(object sender, System.EventArgs e)
{
int iCnt = cmbPath.Items.Count;
SetProgressBar(iCnt);
for(int i=0; i<iCnt; i++)
{
Object selectedItem = cmbPath.Items[i];
sOriginateName = selectedItem.ToString();
sCopyName = sOriginateName + "\\" + DateTime.Now.ToString("yyMMdd_HHmm");
if(!SetFileCopy(sOriginateName, sCopyName))
break;
else
{
pBar1.PerformStep();
}
}
}
}
}
/*
웹서버의 여러개의 bin 폴더를 백업할때 수작업으로 하던 작업을 자동화하기 위해 만들어본 프로그램 입니다.
새롭게 복사되는 폴더명은 "년월일_시분" 또는 "년월일_시분_*"(1씩증가) 형식으로 정해집니다.
*/