Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

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

0
hawks4ever
Asked:
hawks4ever
  • 2
1 Solution
 
grayeCommented:
One of the best ways is to use the low-level APIs to connect to the share before you launch the OpenFileDialog
There is an excellent C# example at the bottom of this article:
http://csharpaspnet.blogspot.com/2008/06/access-unc-file-share-with-credentials.html 
0
 
hawks4everAuthor Commented:
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.
0
 
hawks4everAuthor Commented:
Turns out I was attempting to copy the original file to itself in one of my methods, hence the error.  All is well now.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now