locked
using ftp to access an ftp server in c#

    Question

  • below is a sample I fount and it seems to be working, It's just that I need to change the folder or directory. Can some one tell me how to change directories in C# on a ftp server.

     

    [code]

    // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
       
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());

                Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
       
                reader.Close();
                response.Close();

    [/code]

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Monday, August 22, 2011 1:53 AM

All replies

  • Put the directory in the url.  e.g.:  "ftp://www.contoso.com/somedirectory/"
    Monday, August 22, 2011 2:52 AM
  • Thanks but I all ready tried that. I have the ftp server setup to show the root folder that has folders for categories. After I connect and log in. Is when I need to change folders.

     

    so when I connect to the ftp server. It will have a list of folders or directories that relate to categories. I need to change to that folder for a category.

    if I connect to it using an ftp client. I get a list of folders. Like this

    dfx

    dfx2

    c4

    bhd, and so on

    var Category = "bhd";

    when I try and use "ftp://www.mydomain.com/" + Category + "/", It errors out because the folder isn't there.

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Monday, August 22, 2011 3:00 AM
  • Your ftp server has a list of folders.  You listed the folders.  So it sounds like from your explanation that, by definition, YES, the folder IS there.  

    So your error may have some other explanation.  What is the exact error?

    Monday, August 22, 2011 1:54 PM
  • Your ftp server has a list of folders.  You listed the folders.  So it sounds like from your explanation that, by definition, YES, the folder IS there.  

    So your error may have some other explanation.  What is the exact error?

    Sorry no. error that I can see. All I need to do is change folders. If I ftp log in and I use an ftp client to log in. I see a some folders. Well when I log in the software. It's going to see the folders too. and it wouldn't list the files in each of the folders that I picked. I need an option to change to one of the folders, just one above after I logged in. if it can't change folders, I do get a null error. folder not fount.

    so when I log in I need to go one folder above.

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    • Edited by Joesoft11a Monday, August 22, 2011 6:38 PM updated it
    Monday, August 22, 2011 6:37 PM
  • All I need to do is change folders.

    Try an absolute path by inserting a %2F like this:

    ftp://www.contoso.com/%2Fsomedirectory/somefile.txt

     


    Tuesday, August 23, 2011 1:03 AM
  • All I need to do is change folders.

    Try an absolute path by inserting a %2F like this:

    ftp://www.contoso.com/%2Fsomedirectory/somefile.txt

     



    Thanks, Your still not under standing, any way, Nope that didn't work.. When I connect to the ftp server. It logs me into a folder called maps. In this folder I have other sub folders. I need to change to one of them. What I need to do is send a change dir command and I haven't fount out how to do that,

    after I connect, I have a folder called tools. When I click a radio button label tools. This changes the Category too "tools"

    then I connect to the ftp server and then it lists the files from that sub folder called tools.

     

     

     

     

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Tuesday, August 23, 2011 2:19 AM
  • There has to be a way to send a change directory command to the ftp server. That's all I'm looking for. The option that was giving will not work. It's the way I have the ftp server set up.

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Tuesday, August 23, 2011 10:55 AM
  • Just look at the logs on the FTP server and see what commands were issued.  See what the CWD command issued looks like.  (Set up your own local FTP server to see what your client is doing if you must.)  I really suspect it is something simple like slashes or escaped slashes.

    Review the docs: make sure you're using the API correctly.  Note particularly the bits about credentials, and absolute/relative URI's.

    http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

     

    You still haven't mentioned what the error in the FtpWebResponse is.

     

    If you insist that you need to change directories, then maybe you should look for a different 3rd party Ftp library.

    Tuesday, August 23, 2011 1:51 PM
  • Sorry, I thought I did, It says folder or file not fount. It just tried to read it and fails. And I all so fount a FTP Library and I can't seem to get that to even connect. So I guess I mite have to come up with another idea.All so thanks for the link. How ever it only shows every thing else other then what I need.  There has to be an easy way to do this.

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    • Edited by Joesoft11a Tuesday, August 23, 2011 8:11 PM updated post
    Tuesday, August 23, 2011 8:08 PM
  • Ok, well you guys are not getting the idea. Here let me do this. I posted my code below. Maybe that will help you to under stand. I don't want to list the directories. I want to list the files in the sub directories.

     

    //when the user click on a radio button

    private void Category_tools_Click(object sender, EventArgs e)
            {
                Category = "TOOLS";
                label1.Text = "Category : " + Category;

                GetMaps(Category, listMaps);
                //GetMapsFTP(Category, listMaps);
                //GetMapsFTPClass(Category, listMaps);

            }

    public static void GetMapsFTP(string Category, ListView listb)
    {
               
     FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.frontline-nova.com/");
               
                request.Method = WebRequestMethods.Ftp.ListDirectory;

                request.Credentials = new NetworkCredential("maps@frontline-nova.com", "HIDDEN");

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);

                //need to change directory "Category"

                //Then I need to loop threw each file and display the file name and file size

                ListViewItem list1 = new ListViewItem();

                list1.Text = reader.ReadToEnd();
                list1.SubItems.Add(filename.Length.ToString() + " bytes");
                listb.Items.Add(list1);

                reader.Close();
                response.Close();
    }

     

    Well that mite give some one an idea. The idea is to list the files in the folder. or directory. After I connect. I need to go up one directory "string Category" The files on that string are the files I want to list and then display them on a listview.

    NOTE. When I connect to the ftp server. I'm all ready in a folder called "maps" The directories are all ready there. I just to to list those's files.

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Wednesday, August 24, 2011 1:51 AM
  • Bingo, After playing a round with you sample. I got it to work, This is so cool. any way. The last problem I have is that my software is in windows forms. What I need it to do is to list each file name in a listView and the file size. The file name can be selected for download.

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Wednesday, August 24, 2011 12:10 PM
  • Well I was hoping for a little clue on this. Thanks you

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Thursday, August 25, 2011 3:13 AM
  • Ok, Thanks. I didn't want to start a new thread. I will. All I wanted to do was when I listed the files from the ftp server. I wanted to get the file sizes too. a gain thank you for your help.

    Joe

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Thursday, August 25, 2011 11:04 AM