在芝加哥工作的这段时间里,我经常会找项目发布经理帮忙更新一些文件到多个Server上,我看她每次都是手工来复制,挺辛苦的,今天终于帮她写了一个小工具,使其可以一次性将所有文件复制到所有的Server上,应该会提高她的工作效率。
主要的代码如下:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:/Inetpub/wwwroot/bin";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
// Allow the user to select multiple images.
openFileDialog1.Multiselect = true;
openFileDialog1.Title = "Select Files";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
for (int i = 0; i < (((System.Windows.Forms.FileDialog)(openFileDialog1))).FileNames.Length; i++)
{
strFileName[i] = ((System.Windows.Forms.FileDialog)(openFileDialog1)).FileNames[i].ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string strFilePath1 = textBox1.Text;
string strFilePath2 = textBox2.Text;
string strFilePath3 = textBox3.Text;
foreach(string fileName in strFileName)
{
if (fileName != null)
{
string filePathName1 = strFilePath1 + fileName.Substring(fileName.LastIndexOf("//"));
string filePathName2 = strFilePath2 + fileName.Substring(fileName.LastIndexOf("//"));
string filePathName3 = strFilePath3 + fileName.Substring(fileName.LastIndexOf("//"));
DeleteFile(filePathName1);
DeleteFile(filePathName2);
DeleteFile(filePathName3);
File.Copy(fileName, filePathName1, true);
File.Copy(fileName, filePathName2, true);
File.Copy(fileName, filePathName3, true);
}
else
{
break;
}
}
MessageBox.Show("Save successfully.");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DeleteFile(string destinationFile)
{
if (File.Exists(destinationFile))
{
FileInfo fi = new FileInfo(destinationFile);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;
}
}