Solved

Need help creating an IIS FTP Virtual Directory progrommatically using VB .Net 2005

Posted on 2011-03-16
6
610 Views
Last Modified: 2012-05-11
I've looked about everywhere that I can think of (here included) and I can't seem to find the answer for this question.

How do I create an FTP virtual directory in VB 2005

In my code, I'm using 'Username' as pretty well all of the variables (FTP VD name, and the root folder for the FV)

Here's the code i have that's not working:
    Private Sub sCreateVirtualDir(ByVal Username As String)

        Dim IISSchema As New System.DirectoryServices.DirectoryEntry("IIS://" & Username & "/Schema/AppIsolated")
        Dim CanCreate As Boolean = Not IISSchema.Properties("Syntax").Value.ToString.ToUpper() = "BOOLEAN"
        IISSchema.Dispose()

        If CanCreate Then
            Dim PathCreated As Boolean

            Try
                Dim IISAdmin As New System.DirectoryServices.DirectoryEntry("IIS://" & Username & "/MSFTPSVC/1/Root")

                'If the virtual directory already exists then delete it
                For Each VD As System.DirectoryServices.DirectoryEntry In IISAdmin.Children
                    If VD.Name = Username Then
                        IISAdmin.Invoke("Delete", New String() {VD.SchemaClassName, Username})
                        IISAdmin.CommitChanges()
                        Exit For
                    End If
                Next VD

                'Create and setup new virtual directory
                Dim VDir As System.DirectoryServices.DirectoryEntry = IISAdmin.Children.Add(Username, "Default FTP Site")
                VDir.Properties("Path").Item(0) = "F:\inetpub\ftpdocs\ftpcust\" & Username
                VDir.Properties("AppFriendlyName").Item(0) = Username
                VDir.Properties("AccessRead").Item(0) = True
                VDir.Properties("AccessWrite").Item(0) = True


                'the following are acceptable params
                'INPROC = 0
                'OUTPROC = 1
                'POOLED = 2
                VDir.Invoke("AppCreate", 1)
                VDir.CommitChanges()
            Catch Ex As Exception
                If PathCreated Then
                    System.IO.Directory.Delete("F:\inetpub\ftpdocs\ftpcust\" & Username)
                End If
                Throw Ex
            End Try
        End If
    End Sub

Open in new window


Any ideas would be GREATLY appreciated, as i've been working on this for about 3 1/2 hours this afternoon now...
0
Comment
Question by:TimBare
  • 5
6 Comments
 
LVL 10

Expert Comment

by:pramodsk40
ID: 35152436
are you getting any error, first make sure you have permission for the f drive. Also try do it same drive where Inetput/wwwroot is present i.e c/d drive and see if it works...?
0
 
LVL 2

Author Comment

by:TimBare
ID: 35152533
I definitely have permission (if I run the iisftpdr script, I can create one just fine.)

I have to compile / run the exe on the server, which I don't have access to right now. I'll run it again and post the crash when I get home.

Inetpub is on the f drive.
0
 
LVL 2

Author Comment

by:TimBare
ID: 35153156
here's the screen shot of the crash, and the details

 FTP Crash
 
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable.

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   at CreateFTPUser.Form1.sCreateVirtualDir(String Username)
   at CreateFTPUser.Form1.Btn_CreateFTPUser_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.ButtonBase.OnKeyUp(KeyEventArgs kevent)
   at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
   at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
   at System.Windows.Forms.Control.WmKeyChar(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3615 (GDR.050727-3600)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
CreateFTPUser
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Documents%20and%20Settings/Administrator/Desktop/CreateFTPUser.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3614 (GDR.050727-3600)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.DirectoryServices
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.DirectoryServices/2.0.0.0__b03f5f7f11d50a3a/System.DirectoryServices.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:TimBare
ID: 35159165
Any thoughts?

I've got just about everything else in the app working (checking if the username exists, creating AD user, creating the physical directory), I just can't peg this part of it...
0
 
LVL 2

Accepted Solution

by:
TimBare earned 0 total points
ID: 35167948
I ended up creating a temporary bat file, writing the line of code that i had working (using iisftpdr.vbs) in said batch file, and then executing that batch file, waiting for it to finish, then deleting the temp file. Seems to work, but not exactly what i wanted to have happen.

Here's my workaround:
    Private Sub sCreateFTPVD(ByVal Username As String)
        Dim tempDir As String = System.IO.Path.GetTempPath
        Dim tmpFile As String = tempDir & "temp.bat"
        Dim objWriter As New System.IO.StreamWriter(tmpFile)
        objWriter.WriteLine("iisftpWrite.vbs /create ""Default FTP Site"" " & Username & " F:\inetpub\ftpdocs\ftpcust\" & Username)
        objWriter.Close()
        Dim ExecFile As New System.Diagnostics.ProcessStartInfo(tmpFile)
        ExecFile.WindowStyle = ProcessWindowStyle.Hidden
        Dim ExecFile2 As Process = Process.Start(ExecFile)
        ExecFile2.WaitForExit()
        File.Delete(tmpFile)
    End Sub

Open in new window

0
 
LVL 2

Author Closing Comment

by:TimBare
ID: 35196704
After researching the issue, I found that this was the best solution, until someone comes up with a better one
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
What is an ISAPI filter?   •      It's an assembly (.dll file) that can add or change the way IIS works.   •      They can be enabled globally for your web server or on a site-by-site basis.   When the IIS server receives a request, enabling the ISAPI fi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now