上传文件到共享文件夹

  /// <summary>
        /// 上传文件:要设置共享文件夹是否有创建的权限,否则无法上传文件
        /// </summary>
        /// <param name="fileNamePath">文件路径以及文件名</param>
        /// <param name="urlPath">网络文件夹地址</param>
        /// <param name="User">用户名</param>
        /// <param name="Pwd">密码</param>
        public string UpLoadFile(string fileNamePath, string urlPath, string User, string Pwd)
        {
            string Flg = "false";
            string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"") + 1);//取文件名称
            if (urlPath.EndsWith(@"") == false) urlPath = urlPath + @"";

            urlPath = urlPath + newFileName;

            WebClient myWebClient = new WebClient();
            NetworkCredential cread = new NetworkCredential(User, Pwd, "Domain");
            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);
                if (postStream.CanWrite)
                {
                    postStream.Write(postArray, 0, postArray.Length);
                    Flg = "true";
                    // MessageBox.Show("文件上传成功!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    Flg = "false";
                    //MessageBox.Show("文件上传错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                postStream.Close();
            }
            catch (Exception ex)
            {
                return ex.ToString();
                //MessageBox.Show(ex.Message, "错误");
            }
            return Flg;
        }
原文地址:https://www.cnblogs.com/akonlei/p/5622277.html