欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# transfer local file to remote server based on File.Copy

程序员文章站 2023-11-15 17:07:40
based on https://*.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials ......
using system;
using system.collections.generic;
using system.componentmodel;
using system.linq;
using system.runtime.interopservices;
using system.text;
using system.threading.tasks;

namespace tfcp
{
    /// <summary>
    /// provides access to a network share.
    /// </summary>
    public class networkshareaccesser : idisposable
    {
        private string _remoteuncname;
        private string _remotecomputername;

        public string remotecomputername
        {
            get
            {
                return this._remotecomputername;
            }
            set
            {
                this._remotecomputername = value;
                this._remoteuncname = @"\\" + this._remotecomputername;
            }
        }

        public string username
        {
            get;
            set;
        }
        public string password
        {
            get;
            set;
        }

        #region consts

        private const int resource_connected = 0x00000001;
        private const int resource_globalnet = 0x00000002;
        private const int resource_remembered = 0x00000003;

        private const int resourcetype_any = 0x00000000;
        private const int resourcetype_disk = 0x00000001;
        private const int resourcetype_print = 0x00000002;

        private const int resourcedisplaytype_generic = 0x00000000;
        private const int resourcedisplaytype_domain = 0x00000001;
        private const int resourcedisplaytype_server = 0x00000002;
        private const int resourcedisplaytype_share = 0x00000003;
        private const int resourcedisplaytype_file = 0x00000004;
        private const int resourcedisplaytype_group = 0x00000005;

        private const int resourceusage_connectable = 0x00000001;
        private const int resourceusage_container = 0x00000002;


        private const int connect_interactive = 0x00000008;
        private const int connect_prompt = 0x00000010;
        private const int connect_redirect = 0x00000080;
        private const int connect_update_profile = 0x00000001;
        private const int connect_commandline = 0x00000800;
        private const int connect_cmd_savecred = 0x00001000;

        private const int connect_localdrive = 0x00000100;

        #endregion

        #region errors

        private const int no_error = 0;

        private const int error_access_denied = 5;
        private const int error_already_assigned = 85;
        private const int error_bad_device = 1200;
        private const int error_bad_net_name = 67;
        private const int error_bad_provider = 1204;
        private const int error_cancelled = 1223;
        private const int error_extended_error = 1208;
        private const int error_invalid_address = 487;
        private const int error_invalid_parameter = 87;
        private const int error_invalid_password = 1216;
        private const int error_more_data = 234;
        private const int error_no_more_items = 259;
        private const int error_no_net_or_bad_path = 1203;
        private const int error_no_network = 1222;

        private const int error_bad_profile = 1206;
        private const int error_cannot_open_profile = 1205;
        private const int error_device_in_use = 2404;
        private const int error_not_connected = 2250;
        private const int error_open_files = 2401;

        #endregion

        #region pinvoke signatures

        [dllimport("mpr.dll")]
        private static extern int wnetuseconnection(
            intptr hwndowner,
            netresource lpnetresource,
            string lppassword,
            string lpuserid,
            int dwflags,
            string lpaccessname,
            string lpbuffersize,
            string lpresult
            );

        [dllimport("mpr.dll")]
        private static extern int wnetcancelconnection2(
            string lpname,
            int dwflags,
            bool fforce
            );

        [structlayout(layoutkind.sequential)]
        private class netresource
        {
            public int dwscope = 0;
            public int dwtype = 0;
            public int dwdisplaytype = 0;
            public int dwusage = 0;
            public string lplocalname = "";
            public string lpremotename = "";
            public string lpcomment = "";
            public string lpprovider = "";
        }

        #endregion

        /// <summary>
        /// creates a networkshareaccesser for the given computer name. the user will be promted to enter credentials
        /// </summary>
        /// <param name="remotecomputername"></param>
        /// <returns></returns>
        public static networkshareaccesser access(string remotecomputername)
        {
            return new networkshareaccesser(remotecomputername);
        }

        /// <summary>
        /// creates a networkshareaccesser for the given computer name using the given domain/computer name, username and password
        /// </summary>
        /// <param name="remotecomputername"></param>
        /// <param name="domainorcomutername"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public static networkshareaccesser access(string remotecomputername, string domainorcomutername, string username, string password)
        {
            return new networkshareaccesser(remotecomputername,
                                            domainorcomutername + @"\" + username,
                                            password);
        }

        /// <summary>
        /// creates a networkshareaccesser for the given computer name using the given username (format: domainorcomputername\username) and password
        /// </summary>
        /// <param name="remotecomputername"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public static networkshareaccesser access(string remotecomputername, string username, string password)
        {
            return new networkshareaccesser(remotecomputername,
                                            username,
                                            password);
        }

        private networkshareaccesser(string remotecomputername)
        {
            remotecomputername = remotecomputername;

            this.connecttoshare(this._remoteuncname, null, null, true);
        }

        private networkshareaccesser(string remotecomputername, string username, string password)
        {
            remotecomputername = remotecomputername;
            username = username;
            password = password;

            this.connecttoshare(this._remoteuncname, this.username, this.password, false);
        }

        private void connecttoshare(string remoteunc, string username, string password, bool promptuser)
        {
            netresource nr = new netresource
            {
                dwtype = resourcetype_disk,
                lpremotename = remoteunc
            };

            int result;
            if (promptuser)
            {
                result = wnetuseconnection(intptr.zero, nr, "", "", connect_interactive | connect_prompt, null, null, null);
            }
            else
            {
                result = wnetuseconnection(intptr.zero, nr, password, username, 0, null, null, null);
            }

            if (result != no_error)
            {
                throw new win32exception(result);
            }
        }

        private void disconnectfromshare(string remoteunc)
        {
            int result = wnetcancelconnection2(remoteunc, connect_update_profile, false);
            if (result != no_error)
            {
                throw new win32exception(result);
            }
        }

        /// <summary>
        /// performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void dispose()
        {
            this.disconnectfromshare(this._remoteuncname);
        }
    }
}


 static void pathcopytoremotedemo()
        {
             string destfile = @"\\remotepcname\sharedfile";
            string sourcefile = @"c:\myfolder\mytxt.txt";
            string filename = path.getfilename(sourcefile);
            string remotepcname = "remotepcname";
            string remotepcdomainname = "remotepcdomainname";
            string remotepcusername = "remotepcpassword";
            string remotepcuserpwd = "remotepcuserpwd";
            string destfullname = path.combine(destfile, filename);
            using (networkshareaccesser.access(remotepcname, remotepcdomainname, remotepcusername, remotepcuserpwd))
            {
                file.copy(sourcefile, destfullname);
            }
        }

 

based on