C - realize file transfer to network disk
Introduction:
Next, the FTP transfer file is implemented. This article is the same as above. However, the connection is established through cmd. After the connection is established, the operation is also realized through the following steps. Establish the hierarchical structure of files as above, so I won't go into details here. C ා realize FTP upload data
1. Call the main method:
this.ftpOperation.UploadFile(vIMSPath, vUID, vPassword, vLocalPath + "/" + txtFile, txtFile);
2.ftpOperation.cs Implementation and operation methods in files
two point one Method of calling in main method:
public void UploadFile(string vPath, string vUID, string vPassword, string vLocalPath, string file) { bool status = false; // status = connectState(vPath, vUID, vPassword);//adopt cmd Make a connection if (status) { DirectoryInfo theFolder = new DirectoryInfo(vPath + "/" + file); string filename = vLocalPath; Transport(vLocalPath, vPath + "/" + file);//transfer file System.Diagnostics.Process.Start(vPath); } else { MessageBox.Show("Failed to connect!"); } }
two point two To establish a connection by calling cmd:
public static bool connectState(string vPath, string vUID, string vPassword) { 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 " + vPath + " " + vPassword + "/user:" + vUID; 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; MessageBox.Show(ex.Message); } finally { proc.Close(); proc.Dispose(); } return Flag; }
two point three Transfer file:
public static void Transport(string src, string fileName) { FileStream inFileStream = new FileStream(src, FileMode.Open); //if (!Directory.Exists(dst)) //{ // Directory.Move(src,dst); //} FileStream outFileStream = new FileStream(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(src);//Delete local file }