Solved

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

Posted on 2011-03-16
6
623 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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