?
Solved

File copy with Windows App

Posted on 2010-01-12
3
Medium Priority
?
187 Views
Last Modified: 2012-05-08
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
Comment
Question by:hawks4ever
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
graye earned 2000 total points
ID: 26362341
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
 
LVL 3

Author Comment

by:hawks4ever
ID: 26374103
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
 
LVL 3

Author Comment

by:hawks4ever
ID: 26374944
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question