CMDNetFileHelper
/// <Summary> /// connect the remote shared folder /// </ Summary> public class CMDNetFileHelper { /// <Summary> /// connect the remote shared folder /// </ Summary> /// < name = "path"> param path to the remote shared folder </ param> /// <param name = "userName"> username </ param> /// <param name = "passWord"> password </ param> /// <Returns> </ 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> /// stored content to the remote local folder, or download files from the remote to the local folder /// </ Summary> /// <param name = "the src"> to save the file path, if you save a file to a shared folder, this path is the path to a local file, such as: @ "D: \ 1.avi" </ param> /// <param name = "dst"> save the file path name free and extensions </ param> /// <param name = "fileName"> name and extension to save 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 = System.IO.Path.Combine(dst, fileName); FileStream outFileStream = new FileStream(dst, 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(); } }
Call the method
/// <Summary> /// remotely stored file /// </ Summary> /// <param name = "File"> Remote File </ param> public static void SaveRadioFromRemote ( String File) { the try { BOOL Status = to false ; // connection sharing folder Status = CMDNetFileHelper.connectState (RadiosConfig.net_root_file, RadiosConfig.net_user, RadiosConfig.net_pwd); IF (Status) { // directory shared folder DirectoryInfo theFolder =new new the DirectoryInfo (RadiosConfig.net_root_file); // relative to the path of the shared folder String filePath = RadiosConfig.local_file; // Get saved file path String filename = Path.Combine (theFolder.FullName, filePath); // perform a method CMDNetFileHelper. Transport (RadiosConfig.net_root_file + @ " \ " + File, RadiosConfig.local_file, File); } the else { the throw new new Exception ( " Could not connect! " ); } } The catch (Exception EX) { the throw EX; // MessageBox.Show ( "save remote file error, note that the remote server address"); // PLog.Log.WriteError (ex.ToString ()); } }