Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

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

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
TimBare
Asked:
TimBare
  • 5
1 Solution
 
pramodsk40Commented:
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
 
TimBareAuthor Commented:
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
 
TimBareAuthor Commented:
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
TimBareAuthor Commented:
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
 
TimBareAuthor Commented:
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
 
TimBareAuthor Commented:
After researching the issue, I found that this was the best solution, until someone comes up with a better one
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now