Link to home
Start Free TrialLog in
Avatar of hawks4ever
hawks4everFlag for United States of America

asked on

File copy with Windows App

I have a windows application that needs to do a couple of things before a file can be copied from 1 server to another.  When I click a button to browse for a file, I want the default location to be set to a UNC path pointing to a server not in the domain.  The servers the file will be copied to also reside in the DMZ with the same admin user name and password.

1. How do I impersonate the user (with the admin/password) when the application runs so my open file dialog correctly points to the UNC path if the user is not already mapped to the drive?
2. How do I pass that user name and password info to those servers when I do my File.Copy (or is there a better method than File.Copy)?
3. Why when I select the file to copy, do I get an error that the file I selected cannot be copied because it's already in use?
public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Multiselect = false;
            fDialog.InitialDirectory = @"\\server\C$\Programs\Folder\Install\Custom";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                lblUpload.Text = fDialog.FileName;
            }
        }

        private void btnCopyFile_Click(object sender, EventArgs e)
        {
            if (lblUpload.Text == "")
            {
                MessageBox.Show("Select a file to copy");
            }
            else
            {
                try
                {
                    lblDest.Text = "Files copied:";
                    string source = lblUpload.Text;
                    string iDest = "";
                    string oDest = "";
                    if (cbOb.Checked)
                    {
                        iDest = source.Replace("server", "ob");
                        lblDest.Text += "\r\n" + iDest;
                        File.Copy(source, iDest, true);
                        if (cbOver.Checked)
                        {
                            oDest = iDest.Replace("Install", "Backup");
                            lblDest.Text += "\r\n" + oDest;
                            File.Copy(source, oDest, true);
                        }
                    }
                    if (cbOb2.Checked)
                    {
                        iDest = source.Replace("server", "ob2");
                        lblDest.Text += "\r\n" + iDest;
                        File.Copy(source, iDest, true);
                        if (cbOver.Checked)
                        {
                            oDest = iDest.Replace("Install", "Backup");
                            lblDest.Text += "\r\n" + oDest;
                            File.Copy(source, oDest, true);
                        }
                    }
                    if (cbDev.Checked)
                    {
                        iDest = source.Replace("server", "dev");
                        lblDest.Text += "\r\n" + iDest;
                        File.Copy(source, iDest, true);
                        if (cbOver.Checked)
                        {
                            oDest = iDest.Replace("Install", "Backup");
                            lblDest.Text += "\r\n" + oDest;
                            File.Copy(source, oDest, true);
                        }
                    }
                    if (!cbOb.Checked && !cbOb2.Checked && !cbDev.Checked)
                    {
                        MessageBox.Show("You must select at least 1 server to copy this file to.");
                    }
                    else
                    {
                        MessageBox.Show("File copied successfully!");
                    }
                }
                catch (IOException exc)
                {
                    MessageBox.Show("ERROR COPYING FILE: \r\n\r\n" + exc.ToString());
                    lblDest.Text = "";
                }
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hawks4ever

ASKER

Thank you greatly for that link, it solved so many problems for me.  Now if I can just figure out why I keep getting the message that the file is in use by another process, I can move on to other more exciting projects.
Turns out I was attempting to copy the original file to itself in one of my methods, hence the error.  All is well now.