• 上传文件到共享文件夹


      /// <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;
            }
  • 相关阅读:
    关于软件设计中遇到的问题
    关于power shell
    如果TChart的发生异常
    重温gof版《设计模式》中的创建型模式
    如何更好的使用cvs
    存储过程返回临时表的问题
    关于bugzilla与svn结合的配置注意事项
    VC知识点:如何用vc6调试CGI程序
    如何让应用程序托盘化
    符号表
  • 原文地址:https://www.cnblogs.com/akonlei/p/5622277.html
Copyright © 2020-2023  润新知