.net 共享文件写入

方法一


        /// <summary>
        /// 共享目录上传文件(process cmd命令连接)
        /// </summary>
        /// <param name="sourceFile">需要上传的文件地址 示例:@"D\hello.txt"</param>
        /// <param name="shareFile">共享目录文件夹地址 示例@"\\192.168.1.29\shardata"</param>
        /// <param name="user">共享目录用户名</param>
        /// <param name="pwd">共享目录密码</param>
        /// <param name="errMsg">错误信息</param>
        public static void ShareUpload(string sourceFile, string shareFile, string user,string pwd,out string errMsg)
        {
            string fileName = Path.GetFileName(sourceFile);//获取文件名称          
            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 " + shareFile + " " + pwd + " /user:" + user;
                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))
                {
                    //上传本地文件至共享目录
                    FileStream inFileStream = new FileStream(sourceFile, FileMode.Open);
                    FileStream outFileStream = new FileStream(shareDir+"\\"+fileName, 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();
                    File.Delete(sourceFile);//删除原文件
                    proc.Close();
                    proc.Dispose();
                }
                errMsg = errormsg;

            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                proc.Close();
                proc.Dispose();
            }
        }

方法二

注:webClient 方式会出现 操作超时的问题,不推荐使用此方法



        /// <summary>
        /// 共享目录上传文件(WebClient)
        /// </summary>
		/// <param name="sourceFile">本地文件路径 示例:@"D:\hello.txt"</param>
        /// <param name="shareFile">共享目录文件夹地址 示例:@"\\192.168.1.39\sagrde"</param>
        /// <param name="user">共享目录用户名</param>
        /// <param name="pwd">共享目录密码</param>
        /// <param name="errMsg">错误信息</param>
        public static void ShareUpload( string sourceFile,string shareFile, string user, string pwd, out string errMsg)
        {
		    string fileName = Path.GetFileName(sourceFile);//获取文件名称
			sring shareFullPath=shareFile+"\\"+fileName;//共享文件目录
            try
            {
			    FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                byte[] postArray = r.ReadBytes((int)fs.Length);
                WebClient myWebClient = new WebClient();
                myWebClient.Credentials = new NetworkCredential(user, pwd);//指定用户名和密码
                myWebClient.Credentials = CredentialCache.DefaultCredentials;
                Stream postStream = myWebClient.OpenWrite(shareFile, "PUT");
                if (postStream.CanWrite)
                {
                    postStream.Write(postArray, 0, postArray.Length);
                    postStream.Close();
                    fs.Dispose();
                    errMsg = "";
                }
                else
                {
                    errMsg = "共享文件上传没有写入权限";
                    postStream.Close();
                    fs.Dispose();
                }
            }
            catch (Exception ex)
            {
			    postStream.Close();
                fs.Dispose();
                errMsg = ex.Message;
            }
        }

方法三

        /// <summary>
        /// 共享目录上传文件(WebRequest)
        /// </summary>
        /// <param name="sourceFile">需要上传的文件地址 示例:@"D:\hello.txt"</param>
        /// <param name="shareFile">共享目录文件夹地址 示例:@"\\192.168.1.39\sagrde"</param>
        /// <param name="user">共享目录用户名</param>
        /// <param name="pwd">共享目录密码</param>
        /// <param name="errMsg">错误信息</param>
        public void ShareUploadfile(string sourceFile, string shareFile, string user, string pwd, out string errMsg)
        {
            string fileName = Path.GetFileName(sourceFile);//获取文件名称
            string shareFullFile = shareFile + fileName;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            try
            {
                FileInfo fileInfo = new FileInfo(sourceFile);
                FileStream fs = fileInfo.OpenRead();
                var request =(FileWebRequest) WebRequest.Create(new Uri(shareFullFile));
                request.Credentials= new NetworkCredential(user,pwd);
                request.Method = "POST";               
                Stream strm = request.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
                strm.Close();
                fs.Close();
                errMsg = "";
            }
            catch(Exception ex)
            {
                errMsg = ex.Message;
            }
        }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值