Go Back   CodeCall Programming Forum > Software Development > C# Programming
Register Blogs Search Today's Posts Mark Forums Read

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-15-2009, 10:08 AM
Newbie
 
Join Date: Oct 2009
Posts: 2
aspkiddy is an unknown quantity at this point
Another method safer than StreamWriter and how can I save the file on another server

Another method safer than StreamWriter and how can I save the file on another server (asp.net c sharp)

I have a form: when the user fills a form, the application takes this information and creates a data file (.csv) and put it (data file) on the server in a directory (name of this directory is [information]) where the web site is located.

Code:
using (System.IO.StreamWriter SW = new System.IO.StreamWriter(Server.MapPath("save/information/Data_" + strDate + ".csv")))

{
 SW.WriteLine(s.FirstName + ";" + s.LastName + ";" + s.Address1 + ";");
 SW.Close();
}

After, I changed the path for save the file, in a different server than the Web server

Code:
StreamWriter SW = new StreamWriter(@"\\111.222.1.00\c$\Inetpub\wwwroot\site_toto\ save\information\Data_" + strDate + ".csv");
It must disable the firewall on server so that it works. I can’t it.

So instead of filing the file with system Windows, I'd like to put with FTP protocole on another place, but I do not know how I can do.

I have a class (by J.P. Trosclair) ! How can I integrate it?

Is that correct if I do like that ?


Code:
using (FTP ftplib = new FTP(MapPath ("Data_" + strDate + ".csv")));

try 
{
    ftplib.Connect("ftp.toto.com", 
                   "tata", 
                   "pata");
    ftplib.ChangeDir("information/");
}
catch(Exception ex)
{    
    Console.WriteLine(s.FirstName + ";" + s.LastName + ";" + s.Address1 + ";");
}

try
{ 
    int perc =  0;
    

    ftplib.OpenDownload("Data_" + strDate + ".csv", true);
    while(ftplib.DoDownload() > 0) 
    { 
        perc = (int)((ftplib.BytesTotal * 100)  / ftplib.FileSize); 
        Console.Write("\rDownloading: {0}/{1} {2}%", 
          ftplib.BytesTotal, ftplib.FileSize, perc);
        Console.Out.Flush();
    }
    Console.WriteLine("");
}
catch(Exception ex) 
{ 
    Console.WriteLine("");
    Console.WriteLine(ex.Message);
}
What do you think of my integration code that you see above? is that this can work or I'm completely wrong


the beginning of the code should I add [using ftplib]

[(using Systemafter :]

Code:
using ftplib;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-15-2009, 12:26 PM
gaylo565's Avatar
Programming Professional
 
Join Date: May 2007
Location: flagstaff, az
Posts: 251
gaylo565 is a jewel in the roughgaylo565 is a jewel in the roughgaylo565 is a jewel in the rough
Re: Another method safer than StreamWriter and how can I save the file on another ser

I don't know what you have their but it doesn't look like a ftp uploader to me? Here is an example of one that I have used before. It uses a form with a few text boxes and a couple of buttons. You dont have to use the form if you don't need it.
ex:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace FTPUploader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void uploadFile(string FTPAddress, string filePath, string username, string password)
        {
            //Create FTP request
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + Path.GetFileName(filePath));

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            //Load the file
            FileStream stream = File.OpenRead(filePath);
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Close();

            //Upload file
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();

            MessageBox.Show("Uploaded Successfully");
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            btnUpload.Enabled = false;
            Application.DoEvents();

            uploadFile(txtFTPAddress.Text, txtFilePath.Text, txtUsername.Text, txtPassword.Text);
            btnUpload.Enabled = true;
        }

        private void txtFTPAddress_Leave(object sender, EventArgs e)
        {
            if (!txtFTPAddress.Text.StartsWith("ftp://"))
                txtFTPAddress.Text = "ftp://" + txtFTPAddress.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFile1.ShowDialog() == DialogResult.OK)
                txtFilePath.Text = openFile1.FileName;
        }
    }
}
I would have included the source code but I am kind of in a hurry and I need a new copy of winzip to archive before I can add it. Maybe I will post with the complete code for the app in a while. Hope this helps out
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-15-2009, 02:32 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
aspkiddy is an unknown quantity at this point
Re: Another method safer than StreamWriter and how can I save the file on another ser

Thanks Gaylo,

For exemple,
if my ftp adrress is ftp.toto.com and
My folder : ftp.toto.com/information/and

my username : tataand
my password:pata

So I can change your code as like as ? :

Code:
	 private void uploadFile(string FTPAddress, string filePath, string username, string password)
        {
            //Create FTP request
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftp.toto.com + "/" + Path.GetFileName(information/));

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(tata, pata);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            //Load the file
            FileStream stream = File.OpenRead(information/);
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Close();

            //Upload file
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();

            MessageBox.Show("Uploaded Successfully");
        }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-16-2009, 11:56 AM
gaylo565's Avatar
Programming Professional
 
Join Date: May 2007
Location: flagstaff, az
Posts: 251
gaylo565 is a jewel in the roughgaylo565 is a jewel in the roughgaylo565 is a jewel in the rough
Re: Another method safer than StreamWriter and how can I save the file on another ser

The only problem I see with your code now is that you need your whole file name including the folder not just the folder name. Also make sure you change the parameter names in your method defenition so they match up with the ones you are using within your method. In mine it is set up to use a browse button so you can find the proper file through an explorer window and it just adds the file w/directory for me automatically. Also make sure you get rid of any extra /'s as you have whenever you use information. These will make it fail (if you just get rid of the forward slash's and change the names in the parameters to match the names you use in the method than the method should be fixed)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
asp.net, c sharp, class, ftp



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -5. The time now is 08:15 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0