Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 12th, 2010, 06:29 PM
Authorized User
 
Join Date: Jul 2006
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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?


Code:
        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 = "";
                }
            }
        }





Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting a console App from a vb.net windows app RobCarter Pro Visual Basic 2005 4 June 24th, 2009 09:20 AM
implementing more than one windows in an app helloise C# 4 November 10th, 2008 12:42 PM
c# windows App: Copy Rights Version Dotnet 2005 winbala .NET Framework 2.0 2 April 7th, 2008 04:02 AM
Accessing Windows service from a windows app sajid08 C# 1 October 6th, 2006 10:25 AM
Copy a path from Windows Register??? thomaz C# 2 March 4th, 2005 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.