• NET 下载共享文件


    执行

            public static void Run()
            {
                var state = ConnectState(@"\192.168.10.160excel", "fish", "12345");
                if (state)
                {
                    //    共享文件夹的目录  
                    TransportRemoteToLocal(@"\192.168.10.160excel1ff79391090d4e8fa507ada85bae31ec.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();
            }
  • 相关阅读:
    将军与妓
    LINUX SHELL
    LINUX FTP
    Java API的常用包
    PYTHON LOGGING模块
    json转xml
    xml转json
    PYTHON DJANGO学习笔记
    PYTHON爬取66影视的电影下载链接,有搜索功能
    JAVA PROPERTIES配置文件使用方法
  • 原文地址:https://www.cnblogs.com/Cailf/p/9947896.html
Copyright © 2020-2023  润新知