如何使用C#中的File.Copy将文件从PC复制到服务器

时间:2019-06-11 06:58:52

标签: c#

我正在尝试将图像文件从一台计算机复制到另一台计算机,但出现错误,提示用户名和密码不正确。

我已将我的代码放在下面:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    name: DataTypes.STRING,
    email: DataTypes.STRING
  }, {});
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

1 个答案:

答案 0 :(得分:0)

假设您的服务器要求使用用户名和密码进行连接,则可以使用WNetUseConnection from mpr.dll建立连接,然后使用WNetCancelConnection断开连接

我过去曾用它来连接SMB共享以进行数据收集等,并且运行良好:

using System;
using System.Runtime.InteropServices;

namespace Util
{
    public class ConnectSMB : IDisposable
    {
        public string URI { get; private set; }
        public bool Connected { get; private set; } = false;

        public ConnectSMB(string uri, string username, string password)
        {
            string connResult = Native.ConnectToRemote(uri, username, password);

            if (connResult != null)
            {
                URI = $"FAILED: {connResult}";
                Connected = false;
            }
            else
            {
                URI = uri;
                Connected = true;
            }
        }

        public void Dispose()
        {
            Close();
        }

        public void Close()
        {
            if (Connected)
            {
                var disconResult = Native.DisconnectRemote(URI);
                URI = disconResult;
                Connected = false;
            }
        }

        public class Native
        {
            #region Consts
            const int RESOURCETYPE_DISK = 1;
            const int CONNECT_UPDATE_PROFILE = 0x00000001;
            #endregion

            #region Errors
            public enum ENetUseError
            {
                NoError = 0,
                AccessDenied = 5,
                AlreadyAssigned = 85,
                BadDevice = 1200,
                BadNetName = 67,
                BadProvider = 1204,
                Cancelled = 1223,
                ExtendedError = 1208,
                InvalidAddress = 487,
                InvalidParameter = 87,
                InvalidPassword = 1216,
                MoreData = 234,
                NoMoreItems = 259,
                NoNetOrBadPath = 1203,
                NoNetwork = 1222,
                BadProfile = 1206,
                CannotOpenProfile = 1205,
                DeviceInUse = 2404,
                NotConnected = 2250,
                OpenFiles = 2401
            }
            #endregion

            #region API methods
            [DllImport("Mpr.dll")]
            private static extern ENetUseError WNetUseConnection(
                IntPtr hwndOwner,
                NETRESOURCE lpNetResource,
                string lpPassword,
                string lpUserID,
                int dwFlags,
                string lpAccessName,
                string lpBufferSize,
                string lpResult
            );

            [DllImport("Mpr.dll")]
            private static extern ENetUseError WNetCancelConnection2(
                string lpName,
                int dwFlags,
                bool fForce
            );

            [StructLayout(LayoutKind.Sequential)]
            private class NETRESOURCE
            {
                public int dwScope = 0;
                // Resource Type - disk or printer
                public int dwType = RESOURCETYPE_DISK;
                public int dwDisplayType = 0;
                public int dwUsage = 0;
                // Local Name - name of local device (optional, not used here)
                public string lpLocalName = "";
                // Remote Name - full path to remote share
                public string lpRemoteName = "";
                public string lpComment = "";
                public string lpProvider = "";
            }
            #endregion

            public static string ConnectToRemote(string remoteUNC, string username, string password)
            {
                NETRESOURCE nr = new NETRESOURCE { lpRemoteName = remoteUNC };
                ENetUseError ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
                return ret == ENetUseError.NoError ? null : ret.ToString();
            }

            public static string DisconnectRemote(string remoteUNC)
            {
                ENetUseError ret = WNetCancelConnection2(remoteUNC, CONNECT_UPDATE_PROFILE, false);
                if (ret == ENetUseError.NoError) return null;
                return ret == ENetUseError.NoError ? null : ret.ToString();
            }
        }
    }
}

通常,我将其包装在一次性类中,该类首先测试直接访问。