ASP.NET访问网络驱动器(映射磁盘)

在程序中用指定账户创建映射,并用该账户运行IIS进程
1、在文件服务器上创建共享目录,并新建访问账户。

比如共享目录为:\192.168.0.9\share
访问账户为:user-1 密码为:123456

2、在WEB服务器上新建用户:user-1 密码为:123456,用户组选择默认的user组即可。
3、在WEB项目中新建公共类WNetHelper
using System.Runtime.InteropServices;
 
public class WNetHelper
    {
        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
        private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
      
        [StructLayout(LayoutKind.Sequential)]
        public class NetResource
        {
            public int dwScope;

            public int dwType;

            public int dwDisplayType;

            public int dwUsage;

            public string lpLocalName;

            public string lpRemoteName;

            public string lpComment;

            public string lpProvider;
        }
        

        /// <summary>
        /// 为网络共享做本地映射
        /// </summary>
        /// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1\user-1)</param>
        /// <param name="password">访问用户密码</param>
        /// <param name="remoteName">网络共享路径(如:\\192.168.0.9\share)</param>
        /// <param name="localName">本地映射盘符</param>
        /// <returns></returns>
        public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
        {
            NetResource netResource = new NetResource();
            netResource.dwScope = 2;
            netResource.dwType = 1;
            netResource.dwDisplayType = 3;
            netResource.dwUsage = 1;
            netResource.lpLocalName = localName;
            netResource.lpRemoteName = remoteName.TrimEnd('\\');
            uint result = WNetAddConnection2(netResource, password, username, 1);
            return result;
        }
}
4、为IIS指定运行账户user-1

要实现此功能,有两种办法:
a) 在web.config文件中的<system.web>节点下,添加如下配置:
b) 在WEB项目中添加公用类LogonImpersonate

 public class LogonImpersonate: IDisposable
    {
        static public string DefaultDomain
        {
            get
            {
                return ".";
            }
        }

        const int LOGON32_LOGON_INTERACTIVE = 2;
        const int LOGON32_PROVIDER_DEFAULT = 0;

        [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
        extern static int FormatMessage(int flag, ref IntPtr source, int msgid, int langid, ref string buf, int size, ref IntPtr args);

        [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
        extern static bool CloseHandle(IntPtr handle);

        [System.Runtime.InteropServices.DllImport("Advapi32.dll", SetLastError = true)]
        extern static bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken
        );

        IntPtr token;
        System.Security.Principal.WindowsImpersonationContext context;

        public LogonImpersonate(string username, string password)
        {
            if (username.IndexOf("\\") == -1)
            {
                Init(username, password, DefaultDomain);
            }
            else
            {
                string[] pair = username.Split(new char[] { '\\' }, 2);
                Init(pair[1], password, pair[0]);
            }
        }
        public LogonImpersonate(string username, string password, string domain)
        {
            Init(username, password, domain);
        }
        void Init(string username, string password, string domain)
        {
            if (LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token))
            {
                bool error = true;
                try
                {
                    context = System.Security.Principal.WindowsIdentity.Impersonate(token);
                    error = false;
                }
                finally
                {
                    if (error)
                        CloseHandle(token);
                }
            }
            else
            {
                int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

                IntPtr tempptr = IntPtr.Zero;
                string msg = null;

                FormatMessage(0x1300, ref tempptr, err, 0, ref msg, 255, ref tempptr);

                throw (new Exception(msg));
            }
        }
        ~LogonImpersonate()
        {
            Dispose();
        }
        public void Dispose()
        {
            if (context != null)
            {
                try
                {
                    context.Undo();
                }
                finally
                {
                    CloseHandle(token);
                    context = null;
                }
            }
        }
    }

在访问映射磁盘之前首先调用此类为IIS更换运行用户:LogonImpersonate imper = new LogonImpersonate(“user-1”, “123456”);

5、在访问共享目录前,调用WNetHelper.WNetAddConnection,添加磁盘映射
public static bool CreateDirectory(string path)
{
    uint state = 0;
    if (!Directory.Exists("Z:"))
    {
        state = WNetHelper.WNetAddConnection(@"comp-1\user-1", "123456", @"\\192.168.0.9\share", "Z:");
    }
    if (state.Equals(0))
    {
        Directory.CreateDirectory(path);
        return true;
    }
    else
    {
        throw new Exception("添加网络驱动器错误,错误号:" + state.ToString());
    }
}
6、完成。

简洁代码就是:
LogonImpersonate imper = new LogonImpersonate(“user-1”, “123456”);
WNetHelper.WNetAddConnection(@“comp-1\user-1”, “123456”, @"\192.168.0.9\share", “Z:”);
Directory.CreateDirectory(@“Z:\newfolder”);
file1.SaveAs(@“Z:\newfolder\test.jpg”);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET逻辑驱动器数源代码是指在ASP.NET的应用程序中,将某个特定的文件夹作为逻辑驱动器,并使用该驱动器访问和操作文件。 在ASP.NET中,可以通过使用Server.MapPath方法将一个特定的文件夹路径映射为逻辑驱动器。在代码中,可以使用以下方式来实现: string path = Server.MapPath("~/文件夹路径/"); 这里的"文件夹路径"是指要映射的文件夹的路径,使用“~”符号表示相对于应用程序的根目录。 接下来,我们可以使用System.IO命名空间中的各种类和方法处理和操作映射的文件夹。例如,可以使用Directory类来获取文件夹中的文件和子文件夹列表,使用File类来读取、写入和删除文件。 以下是通过逻辑驱动器访问和操作文件的示例代码: // 获取文件夹中的文件列表 string[] fileNames = Directory.GetFiles(path); // 遍历文件列表并读取每个文件的内容 foreach (string fileName in fileNames) { string content = File.ReadAllText(fileName); // 处理文件内容 // ... } // 创建一个新的文件并写入内容 string newFileName = path + "newfile.txt"; File.WriteAllText(newFileName, "Hello, World!"); // 删除一个文件 string fileToDelete = path + "todelete.txt"; File.Delete(fileToDelete); 通过逻辑驱动器,在ASP.NET的应用程序中可以方便地访问和操作特定的文件夹。这样,开发人员可以更加灵活地处理文件和文件夹,进一步实现自己的需求和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值