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

ASPNET 下载共享文件

程序员文章站 2022-07-31 20:03:58
执行 链接 下载 ......

执行

        public static void run()
        {
            var state = connectstate(@"\\192.168.10.160\excel\", "fish", "12345");
            if (state)
            {
                //    共享文件夹的目录  
                transportremotetolocal(@"\\192.168.10.160\excel\1ff79391090d4e8fa507ada85bae31ec.xlsx", @"d:\fish-a1.xlsx");
            }
        }

链接

/// <summary>  
        /// 连接远程共享文件夹  
        /// </summary>  
        /// <param name="path">远程共享文件夹的路径</param>  
        /// <param name="username">用户名</param>  
        /// <param name="password">密码</param>  
        /// <returns></returns>  
        public static bool connectstate(string path, string username, string password)
        {
            bool flag = false;
            process proc = new process();
            try
            {
                proc.startinfo.filename = "cmd.exe";
                proc.startinfo.useshellexecute = false;
                proc.startinfo.redirectstandardinput = true;
                proc.startinfo.redirectstandardoutput = true;
                proc.startinfo.redirectstandarderror = true;
                proc.startinfo.createnowindow = true;
                proc.start();
                string dosline = "net use " + path + " " + password + " /user:" + username;
                proc.standardinput.writeline(dosline);
                proc.standardinput.writeline("exit");
                while (!proc.hasexited)
                {
                    proc.waitforexit(1000);
                }
                string errormsg = proc.standarderror.readtoend();
                proc.standarderror.close();
                if (string.isnullorempty(errormsg))
                {
                    flag = true;
                }
                else
                {
                    throw new exception(errormsg);
                }
            }
            catch (exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.close();
                proc.dispose();
            }
            return flag;
        }

下载

/// <summary>  
        /// 从远程服务器下载文件到本地  
        /// </summary>  
        /// <param name="savesrc">保存到本地的路径:下载到本地后的文件路径,包含文件的扩展名</param>  
        /// <param name="sourcesrc">远程服务器路径(共享文件夹路径)+ 远程服务器(共享文件夹)中的文件名称,包含扩展名</param>  
        public static void transportremotetolocal(string sourcesrc, string savesrc)
        {
            //  远程服务器文件  此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错 
            if (!file.exists(sourcesrc))
                return; 
            filestream infilestream = file.openread(sourcesrc);
            //  从远程服务器下载到本地的文件  
            filestream outfilestream = new filestream(savesrc, filemode.openorcreate);
            byte[] buf = new byte[infilestream.length];
            int bytecount;
            while ((bytecount = infilestream.read(buf, 0, buf.length)) > 0)
            {
                outfilestream.write(buf, 0, bytecount);
            }
            infilestream.flush();
            infilestream.close();
            outfilestream.flush();
            outfilestream.close();
        }