向多个Server上拷贝文件

在芝加哥工作的这段时间里,我经常会找项目发布经理帮忙更新一些文件到多个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;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值