aspx Tutorial

.NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services, .NET articles, Sharepoint 2010, visual studio 2010,Aamir Hasan,IT
Advertise Here

Advertize

wwwSW
Posted by Aamir Hasan   on Friday, November 5, 2010 Total Views:  

In this example, i have told you how to create a directory on FTP Server using C#/VB.NET.Let's start

Namespaces required:

C#

using System.Net;
using System.IO;

VB.NET

Imports System.Net
Imports System.IO

Following is the code of create directory on the FTP Server.

C#

private void MakeDir(string dirName)
   {
       FTPSettings.IP = "DOMAIN NAME";
       FTPSettings.UserID = "USER ID";
       FTPSettings.Password = "PASSWORD";
       FtpWebRequest reqFTP = null;
       Stream ftpStream = null;
       try
       {
           reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/" + dirName));
           reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
           reqFTP.UseBinary = true;
           reqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
           FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
           ftpStream = response.GetResponseStream();
           ftpStream.Close();
           response.Close();
       }
       catch (Exception ex)
       {
           if (ftpStream != null)
           {
               ftpStream.Close();
               ftpStream.Dispose();
           }
           throw new Exception(ex.Message.ToString());
       }
   }

   public static class FTPSettings
   {
       public static string IP { get; set; }
       public static string UserID { get; set; }
       public static string Password { get; set; }
   }

VB.NET

  Private Sub MakeDir(ByVal dirName As String)
       FTPSettings.IP = "DOMAIN NAME"
       FTPSettings.UserID = "USER ID"
       FTPSettings.Password = "PASSWORD"
       Dim reqFTP As FtpWebRequest = Nothing
       Dim ftpStream As Stream = Nothing
       Try
           reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + dirName)), FtpWebRequest)
           reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory
           reqFTP.UseBinary = True
           reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
           Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
           ftpStream = response.GetResponseStream()
           ftpStream.Close()
           response.Close()
       Catch ex As Exception
           If ftpStream IsNot Nothing Then
               ftpStream.Close()
               ftpStream.Dispose()
           End If
           Throw New Exception(ex.Message.ToString())
       End Try
   End Sub

   Public NotInheritable Class FTPSettings
       Private Sub New()
       End Sub
       Public Shared Property IP() As String
           Get
               Return m_IP
           End Get
           Set(ByVal value As String)
               m_IP = Value
           End Set
       End Property
       Private Shared m_IP As String
       Public Shared Property UserID() As String
           Get
               Return m_UserID
           End Get
           Set(ByVal value As String)
               m_UserID = Value
           End Set
       End Property
       Private Shared m_UserID As String
       Public Shared Property Password() As String
           Get
               Return m_Password
           End Get
           Set(ByVal value As String)
               m_Password = Value
           End Set
       End Property
       Private Shared m_Password As String
   End Class

Download

CreateDirectory.zip.zip (1.10 kb)

You can use this function in both asp.net and Windows Form csharp/VB.Net Application.

Category: All | asp.net | CSharp | FTP
Protected by Copyscape Online Plagiarism Tool

Comments (5) -

aamirhasan
aamirhasan United States
11/5/2010 5:17:01 AM #

If you know better solution then please post here to share with us

CR Lithium battery
CR Lithium battery United States
6/8/2011 11:55:15 PM #

Advertising may be described as the science of arresting the human intelligence long enough to get money from it  ,yes

male enhancement
male enhancement United States
10/24/2011 4:26:23 AM #

great post.. i really enjoyed it

David Connolly
David Connolly United Kingdom
12/14/2011 5:07:28 PM #

Great, this was exactly what I was looking for.  To expand on what you've posted, before creating a directory, I'd like to check if it's already there (obviously no need to run the create if it already exists and could result in error anyway).  How would I go about that?

David Connolly
David Connolly United Kingdom
12/14/2011 5:07:50 PM #

Great, this was exactly what I was looking for.  To expand on what you've posted, before creating a directory, I'd like to check if it's already there (obviously no need to run the create if it already exists and could result in error anyway).  How would I go about that?

fafafa
fafafa Slovenia
4/23/2012 3:03:34 AM #

http://www.christianlouboutinukk.org   christian louboutin
http://www.tiffanyuks.org   tiffany uk
http://www.gucciukbeltuk.org   gucci belt
http://www.frsaclouisvuittonsac.com   louis vuitton sac
http://www.chanelukoutletuks.org   chanel outlet
http://www.burberryukoutletuk.org   burberry
http://www.poloralphlaurenuko.org   ralph lauren uk

Christian Louboutin Australia
Christian Louboutin Australia People's Republic of China
5/8/2012 5:04:41 AM #

You'll find a handful of things you'll want to know about
http://www.christianlouboutinshoeau.com/ Christian Louboutin Australia jacket and pant types when hunting for the best http://www.clouboutinshoesuksale.com/  Christian Louboutin UK suit since based on your personal http://www.michaelkorsoutletpop.com/ Michael Kors Outlet style, weight, height, physique type, the occasion plus a few other factors, the style of suit you buy http://www.nikedunkpascher.fr/ Nike Dunk Pas Cher makes a distinction. By style I mean this: Suits come in http://www.nikerunninguk.com/ Nike Running Shoes UK two or three-piece designs.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5