• C# 向共享文件夹上传及下载文件


    //第一步建立共享链接

    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 + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";
                    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;
            }

    //第二部上传文件

    public void UpLoadFile(string fileNamePath, string urlPath, string User, string Pwd)
            {
                string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"") + 1);//取文件名称
                MessageBox.Show(newFileName);
                if (urlPath.EndsWith(@"") == false) urlPath = urlPath + @"";
    
                urlPath = urlPath + newFileName;
    
                WebClient myWebClient = new WebClient();
                
                NetworkCredential cread = new NetworkCredential();
                
                myWebClient.Credentials = cread;
                FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
    
                try
                {
                    byte[] postArray = r.ReadBytes((int)fs.Length);
                    Stream postStream = myWebClient.OpenWrite(urlPath);
                    // postStream.m
                    if (postStream.CanWrite)
                    {
                        postStream.Write(postArray, 0, postArray.Length);
                        MessageBox.Show("文件上传成功!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("文件上传错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
    
                    postStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误");
                }
    
            }

    //下载文件

    public void DownLoadFile(string URL, string DIR)
            {
                string FileName = URL.Substring(URL.LastIndexOf("\") + 1);
                string PATH = DIR + FileName;
                try
                {
                    WebRequest SC = WebRequest.Create(URL);
                }
                catch
                {
                }
                try
                {
                    //client.DownloadFile(URL, PATH);
                }
                catch
                {
                }
            }

    //网上找了好多资料   都不很很符合我的要求 改成这种方法可以实现 局域网图片上传 很爽 大家可以尝试一下

  • 相关阅读:
    osg::BlendFunc来设置透明度
    LCA(Tarjan)
    CODEVS1073 家族 (并查集)
    CODEVS1533 互斥的数(哈希表)
    2014-12-4
    BZOJ2661 连连看 (费用流)
    2014-11-30
    JAVA语法基础作业——动手动脑以及课后实验性问题
    课后作业01——相加
    再读大道至简第二章
  • 原文地址:https://www.cnblogs.com/codedreams/p/5258776.html
Copyright © 2020-2023  润新知