Example #1
0
        /// <summary>
        /// Create a mapped drive pointing to Azure files.
        /// </summary>
        /// <param name="driveLetter"></param>
        /// <param name="filesPath"></param>
        /// <param name="accountName"></param>
        /// <param name="accountKey"></param>
        /// <param name="force"></param>
        public static void Mount(string driveLetter, string filesPath, string accountName, string accountKey, bool force = true)
        {
            if (String.IsNullOrEmpty(filesPath))
            {
                throw new ArgumentException("The filesPath is required.", "filesPath");
            }
            if (String.IsNullOrEmpty(accountName))
            {
                throw new ArgumentException("The accountName is required.", "accountName");
            }
            if (String.IsNullOrEmpty(accountKey))
            {
                throw new ArgumentException("The accountKey is required.", "accountKey");
            }
            driveLetter = ParseDriveLetter(driveLetter);

            // Define the new resource.
            var resource = new NETRESOURCE
            {
                dwScope       = (ResourceScope)2,
                dwType        = (ResourceType)1,
                dwDisplayType = (ResourceDisplayType)3,
                dwUsage       = (ResourceUsage)1,
                lpRemoteName  = filesPath,
                lpLocalName   = driveLetter
            };

            // Close connection if it exists.
            if (force)
            {
                NetworkApi.WNetCancelConnection2(driveLetter, 0, true);
            }

            // Create the connection.
            var result = NetworkApi.WNetAddConnection2(resource, accountKey, accountName, 0);

            if (result != 0)
            {
                throw new FilesMappedDriveException(String.Format(MountError, driveLetter, filesPath, (SYSTEM_ERROR)result), result);
            }
        }