当前位置:网站首页>C # upload files to shared folders
C # upload files to shared folders
2022-07-22 16:25:00 【Cherry Blossom】
Use C# Upload files to shared folders in the LAN
- Source code is as follows :
public void Main() { OpenFileDialog openFileDialogTemp = new OpenFileDialog(); openFileDialogTemp.Title = " Select the picture to upload "; //openFileDialogTemp.Filter = "*.jpg,*.png,|*.jpg;*.png;"; DialogResult dr = openFileDialogTemp.ShowDialog(); if (!File.Exists(openFileDialogTemp.FileName)) { GLOBALS.msgbox(" Photo is empty , Please choose a picture "); return; } if (dr == DialogResult.OK) { txtFilePath.Text = openFileDialogTemp.FileName; string filePath = this.txtFilePath.Text; } // Connect shared folders bool status = connectState(@"\\IP\uploadImages", " Login name ", " The login password "); if (status) { // Shared folder Directory DirectoryInfo theFolder = new DirectoryInfo(@"\\IP\uploadImages\"); // Get the path to save the file string PathName = theFolder.ToString(); // Execution method Transport(txtFilePath.Text, PathName, "4.txt"); // Upload files to a shared folder on another computer , You need to set the permissions of the shared folder , Do not set shared folders on the desktop //TransportRemoteToLocal(@"D:\Test2.txt", PathName, "2.txt"); // Download files from a shared folder on another computer to this computer } else { } Console.ReadKey(); }
-
/// <summary> /// Save local content to a remote folder , Or download files from remote folders to local /// </summary> /// <param name="src"> The path of the file to save , If you save files to a shared folder , This path is the local file path </param> /// <param name="dst"> Path to save file , No name and extension </param> /// <param name="fileName"> Save the name and extension of the file </param> public static void Transport(string src, string dst, string fileName) { FileStream inFileStream = new FileStream(src, FileMode.Open); if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } dst = dst + fileName; if (!File.Exists(dst)) { FileStream outFileStream = new FileStream(dst, FileMode.Create, FileAccess.Write); 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(); } }
/// <summary> /// Connect shared files /// </summary> /// <param name="path"> Shared file address </param> /// <param name="userName"> user name </param> /// <param name="passWord"> password </param> /// <returns>true: Successful connection false: The connection fails </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> /// Download files from remote server to local /// </summary> /// <param name="src"> The file path after downloading to the local , Contains the extension of the file </param> /// <param name="dst"> Remote server path ( Shared folder path )</param> /// <param name="fileName"> remote server ( Shared folders ) File name in , Include extension </param> public static void TransportRemoteToLocal(string src, string dst, string fileName) //src: The file path after downloading to the local dst: Remote server path fileName: remote server dst File name under path { if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } dst = dst + fileName; FileStream inFileStream = new FileStream(dst, FileMode.Open); // Remote server files It is assumed that this file is indeed contained in the shared folder of the remote server , Otherwise, the program reports an error FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate); // File downloaded from remote server to local 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(); }
边栏推荐
- Segment tree learning record
- SAP 电商云 Spartacus UI 客户系统的跨域请求
- 12 nodes of DOM
- The principle of distributed transaction is simple, and it is full of holes
- Lateral biting function provided by wisdom teeth
- RquestMapping的注解功能、注解使用范围、注解的属性详情
- Evening Sky
- 操作教程:大华摄像头通过GB28181协议注册EasyCVR平台的详细配置
- 学生如何提高专业英文阅读能力丨传道授业
- 嵌套子查询
猜你喜欢
[solution] NPM err! code E401
NFT 多样化的发行方式
PXE网络装机
Matlab simulation of BER performance of rs+bch concatenated coding and decoding
The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
EasyCVR平台如何实现无需鉴权即可接入特殊设备?
DOM style operation
sftp创建
C#服务器NFS共享文件夹搭建与上传图片文件
Signal FFT, STFT, wavelet transform, envelope analysis, etc
随机推荐
Glide 源码解析
NFT 多样化的发行方式
【Unity项目实践】游戏架构
Scala variables and data types (2)
The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
MySQL starts the global log to locate and troubleshoot slow SQL
St table (jump table)
DOM browser rendering process and repaint reflow
The appearance sequence of question 38 in C language. Three methods (traversal method, recursive method and wolf killing method)
[solution] NPM err! code E401
这个好用的办公网优化工具,官宣免费了
Mo team problem sorting
Mock模拟数据,并发起get,post请求(保姆级教程,一定能成功)
AT5662 [AGC040D] Balance Beam(二分)
About the recent online treatment of myopia with low concentration atropine
网络相关概念面试题
Session共享问题
盒马两大供应链中心启用 多业态商品创新研发“有后台”
Summary of product related issues (Part 1)
什么是单片机?单片机的组成部分有哪些?