private void Download(FileContainer f) { // if user doesn't use a robot ip address, don't try to upload if (TextBoxServer.Text.Length == 0) { return; } try { // Get the object used to communicate with the server. string req_string = "ftp://" + TextBoxServer.Text + m_SettingsPath + f.m_Filename; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(req_string); request.Method = WebRequestMethods.Ftp.DownloadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential("anonymous", "987Rules"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); f.m_Text = reader.ReadToEnd(); Console.WriteLine("Download Complete, status {0}", response.StatusDescription); reader.Close(); response.Close(); f.m_Modified = true; } catch { TextBoxServer.Text = ""; } }
private bool SaveLocal(FileContainer fcon) { string local_fname = GetLocalPath(); local_fname += fcon.m_Filename; System.IO.File.WriteAllText(local_fname, fcon.m_Text); return(true); }
private void FileEdit_TextChanged(object sender, EventArgs e) { string filename = Get_Current_Filename(); if (m_Files.ContainsKey(filename)) { FileContainer fcon = m_Files[filename]; fcon.m_Text = FileEdit.Text; fcon.m_Modified = true; } }
private void ButtonLoadRobot_Click(object sender, EventArgs e) { // scan for files on the host and download or update any of them string req_string = "ftp://" + TextBoxServer.Text + m_SettingsPath + "*.hrs"; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(req_string); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential("anonymous", "987Rules"); try { FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); List <string> filenames = new List <string>(); while (!reader.EndOfStream) { string fname = reader.ReadLine(); filenames.Add(fname); } response.Close(); foreach (string fname in filenames) { FileContainer fcon = null; if (!m_Files.ContainsKey(fname)) { fcon = new FileContainer(); fcon.m_Filename = fname; m_Files.Add(fcon.m_Filename, fcon); } else { fcon = m_Files[fname]; } Download(fcon); SaveLocal(fcon); } Update_File_List_Combo(); } catch { MessageBox.Show(TextBoxServer.Text + " could not be reached."); TextBoxServer.Text = ""; } }
// byte[] toBytes = Encoding.ASCII.GetBytes(somestring); // string something = Encoding.ASCII.GetString(toBytes); private bool Upload(FileContainer f) { // if user doesn't use a robot ip address, don't try to upload if (TextBoxServer.Text.Length == 0) { return(false); } try { // Get the object used to communicate with the server. string req_string = "ftp://" + TextBoxServer.Text + m_SettingsPath + f.m_Filename; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(req_string); request.ReadWriteTimeout = 3000; request.Timeout = 1000; request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential("anonymous", "987Rules"); // Copy the contents of the file to the request stream. byte[] fileContents = Encoding.ASCII.GetBytes(f.m_Text); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); f.m_Modified = false; } catch { TextBoxServer.Text = ""; // don't show a message box on every file... return(false); } return(true); }
private void ButtonLoadLocal_Click(object sender, EventArgs e) { // get list of HRS files in the local directory string[] filenames = Directory.GetFiles(GetLocalPath(), "*.hrs"); foreach (string fname in filenames) { FileContainer fcon = null; if (!m_Files.ContainsKey(fname)) { fcon = new FileContainer(); fcon.m_Filename = Path.GetFileName(fname); m_Files.Add(fcon.m_Filename, fcon); } else { fcon = m_Files[fname]; } LoadLocal(fcon); fcon.m_Modified = true; } Update_File_List_Combo(); }
private void LoadLocal(FileContainer fcon) { string local_fname = GetLocalPath() + fcon.m_Filename; fcon.m_Text = System.IO.File.ReadAllText(local_fname); }