Solved

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

Posted on 2011-03-16
6
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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