Re: Write File to Network Drive using Wi Fi connection

From: Robert J (paa_at_bigpond.com)
Date: 06/23/04


Date: Wed, 23 Jun 2004 23:21:56 GMT

I did a Google Search in this Newsgroup for WNetAddConnection but
only found this thread, where and how do I search in the archives ?

Going down a different path shelling out to another program on the server
using Stored Proc xp_cmdshell, but I am sure something like this will come
up
again.

I did found the declarations for the Functions and a Class to find existing
connections. I'm not familiar with P\Invoke have heard it mentioned though,
is it only used for the C family of languages ?

Well here is what I found in VB.Net for anyone interested.

Declare Function WNetAddConnection Lib "mpr.dll" Alias
"WNetAddConnectionA"(ByVal lpszNetPath As String, ByVal lpszPassword As
String, ByVal lpszLocalName As String) As Integer

Declare Function WNetAddConnection2 Lib "mpr.dll" Alias
"WNetAddConnection2A"(ByRef lpNetResource As NETRESOURCE, ByVal lpPassword
As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

  Public Class clsTestWNetGetConnection
#Region "Costanti"
    Private Const DRIVE_UNKNOWN = 0
    Private Const DRIVE_ABSENT = 1
    Private Const DRIVE_REMOVABLE = 2
    Private Const DRIVE_FIXED = 3
    Private Const DRIVE_REMOTE = 4
    Private Const DRIVE_CDROM = 5
    Private Const DRIVE_RAMDISK = 6
    ' returns errors for UNC Path
    Private Const ERROR_BAD_DEVICE = 1200&
    Private Const ERROR_CONNECTION_UNAVAIL = 1201&
    Private Const ERROR_EXTENDED_ERROR = 1208&
    Private Const ERROR_MORE_DATA = 234
    Private Const ERROR_NOT_SUPPORTED = 50&
    Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&
    Private Const ERROR_NO_NETWORK = 1222&
    Private Const ERROR_NOT_CONNECTED = 2250&
    Private Const NO_ERROR = 0
#End Region
#Region "API"
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias
"GetLogicalDriveStringsA" (ByVal nBufferLength As Integer, ByVal lpBuffer As
String) As Integer
    Private Declare Function GetDriveType Lib "kernel32" Alias
"GetDriveTypeA" (ByVal nDrive As String) As Integer
    Private Declare Function WNetGetConnection Lib "mpr.dll" Alias
"WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As
String, ByRef cbRemoteName As Integer) As Integer
#End Region
#Region "Public Method"

    Public function sListAllDrives() As String 'Show Drive Letter and Phisic
Path
        Dim strAllDrives As String
        Dim strTmp As String
        Dim Ret As String

        strAllDrives = fGetDrives()
        If strAllDrives <> "" Then
            Do
                strTmp = Mid(strAllDrives, 1,
strAllDrives.IndexOf(vbNullChar) - 1)
                strAllDrives = Mid(strAllDrives,
strAllDrives.IndexOf(vbNullChar) + 1)
                Select Case fDriveType(strTmp)
                    Case "Removable Media"
                        Ret &= "Removable drive : " & strTmp
                    Case "CD Rom"
                        Ret &= " CD Rom drive : " & strTmp
                    Case "Fixed Drive"
                        Ret &= " Local drive : " & strTmp
                    Case "Network Drive"
                        Ret &= " Network drive : " & strTmp
                        Ret &= " UNC Path : " &
fGetUNCPath(Left$(strTmp, Len(strTmp) - 1))
                End Select
            Loop While strAllDrives <> ""
        End If
        Return Ret
    End Function

#End Region
#Region "Private Method"

    Private Function fGetDrives() As String
        'Returns all mapped drives
        Dim lngRet As Integer
        Dim strDrives As String = New String(Chr(32), 255)
        Dim lngTmp As Integer
        lngTmp = strDrives.Length
        lngRet = GetLogicalDriveStrings(lngTmp, strDrives)
        fGetDrives = Left(strDrives, lngRet)
    End Function

    Private Function fGetUNCPath(ByVal strDriveLetter As String) As String

        Try

            Dim Msg As String
            Dim lngReturn As Integer
            Dim lpszLocalName As String
            Dim lpszRemoteName As String
            Dim cbRemoteName As Integer
            lpszLocalName = strDriveLetter
            lpszRemoteName = New String(Chr(32), 255)
            cbRemoteName = lpszRemoteName.Length
            lngReturn = WNetGetConnection(lpszLocalName, lpszRemoteName,
cbRemoteName)
            Select Case lngReturn
                Case ERROR_BAD_DEVICE
                    Msg = New String("Error: Bad Device")
                Case ERROR_CONNECTION_UNAVAIL
                    Msg = New String("Error: Connection Un-Available")
                Case ERROR_EXTENDED_ERROR
                    Msg = New String("Error: Extended Error")
                Case ERROR_MORE_DATA
                    Msg = New String("Error: More Data")
                Case ERROR_NOT_SUPPORTED
                    Msg = New String("Error: Feature not Supported")
                Case ERROR_NO_NET_OR_BAD_PATH
                    Msg = New String("Error: No Network Available or Bad
Path")

                Case ERROR_NO_NETWORK

                    Msg = New String("Error: No Network Available")
                Case ERROR_NOT_CONNECTED
                    Msg = New String("Error: Not Connected")
                Case NO_ERROR
                    ' all is successful...
            End Select
            If Not (Msg Is Nothing) Then
                MessageBox.Show(Msg, String.Empty, MessageBoxButtons.OK,
MessageBoxIcon.Information)
                Return New String(String.Empty)
            Else
                fGetUNCPath = Left$(lpszRemoteName,
lpszRemoteName.IndexOf(Chr(0)))
            End If
        Catch Ex As Exception
            MessageBox.Show(Ex.Message, String.Empty, MessageBoxButtons.OK,
MessageBoxIcon.Error)
        End Try
    End Function

    Private Function fDriveType(ByVal strDriveName As String) As String
        Dim lngRet As Integer
        Dim strDrive As String = New String(String.Empty)
        lngRet = GetDriveType(strDriveName)
        Select Case lngRet
            Case DRIVE_UNKNOWN 'The drive type cannot be determined.
                strDrive = New String("Unknown Drive Type")
            Case DRIVE_ABSENT 'The root directory does not exist.
                strDrive = New String("Drive does not exist")
            Case DRIVE_REMOVABLE 'The drive can be removed from the drive.
                strDrive = New String("Removable Media")
            Case DRIVE_FIXED 'The disk cannot be removed from the drive.
                strDrive = New String("Fixed Drive")
            Case DRIVE_REMOTE 'The drive is a remote (network) drive.
                strDrive = New String("Network Drive")
            Case DRIVE_CDROM 'The drive is a CD-ROM drive.
                strDrive = New String("CD Rom")
            Case DRIVE_RAMDISK 'The drive is a RAM disk.
                strDrive = New String("Ram Disk")
        End Select
        Return strDrive
    End Function

#End Region
End Class

Cheers

Robert [VPDGOGYD]

"Paul G. Tobey [eMVP]" <ptobey_no_spam@instrument_no_spam.com> wrote in
message news:%23REPpNHWEHA.4024@TK2MSFTNGP12.phx.gbl...
> That is, if you set up the share manually before the first run of your
> application, entering the password, etc. (or having the user do it), if
> necessary, then you'd just assume that there is a folder
\Network\sharename
> on computername, and go from there. If that won't work for you, you'll
have
> to P/Invoke to the WNET routines (WNetAddConnection, etc.). There may
have
> been some traffic on doing this before, so check the newsgroup archives.
>
> Paul T.
>
> "jez" <jezonline@hotmail.com> wrote in message
> news:40d80f67$0$62377$5fc3050@dreader2.news.tiscali.nl...
> > g'd day,
> >
> > this is exactly what I'm using in my application. I "download" and
> "upload"
> > files using the copy() or move() method. It's actually extremely easy to
> use
> > and very fast (using WiFi!).
> >
> > As for the username/password issue. I haven't found (didn't really look
> for
> > it) a solution for that. But you only need to enter the
username/password
> > once and then save it. What I do here is that when I first set up the
> pocket
> > PC for the users I try to access a shared map. The Pocket PC then asks
me
> > for a user/pass/domain, I fill it in and click on the save password
> > checkbox. Never got that dialog box again.
> >
> > I supose that would kind of solve your problem.
> >
> > jez
> >
> > "Robert J" <paa@bigpond.com> wrote in message
> > news:DGKBc.48867$sj4.30757@news-server.bigpond.net.au...
> > > Thanks for the suggestions Paul.
> > > The third option seems like it should be the easiest.
> > > All Servers will be either running NT 4 Server or 2000 Server.
> > >
> > > All the Servers are internal to the company I work for so I could
easily
> > > create the same Network share on them with read\write permission set
> > > for Everyone.
> > >
> > > The problem I have and I can't find any documentation on accessing
that
> > > share using the Compact Framework
> > >
> > > If on each Server I created the share ReportsToPrint wouldn't this
still
> > > have to be
> > > accessed using something like \\MyServer\ReportsToPrint\MyFile.txt ?
> > >
> > > I have just tried to do something like that and using the following
> code:
> > >
> > > Dim filename As String = "\\MyServer\ReportsToPrint\MyFiletxt"
> > >
> > > Dim myFileStream As New System.IO.FileStream(filename, _
> > > System.IO.FileMode.OpenOrCreate, _
> > > System.IO.FileAccess.Write, System.IO.FileShare.None)
> > >
> > > ' create a stream reader attach the file stream to it
> > > Dim myWriter As New System.IO.StreamWriter(myFileStream)
> > >
> > > As Soon as I go to open the FileStream the
> > > Username:
> > > Password:
> > > Domain
> > > pops up on the emulator, probably because the device isn't logged into
> the
> > > Domain.
> > >
> > > Is there any way around this ?
> > >
> > > Cheers
> > >
> > > Robert
> > >
> > > "Paul G. Tobey [eMVP]" <ptobey_no_spam@instrument_no_spam.com> wrote
in
> > > message news:uiVl4r7VEHA.2816@TK2MSFTNGP11.phx.gbl...
> > > > You'll need to investigate quite a bit more thoroughly what will be
> > > > available on the server sides at all of your possible deployment
> > locations
> > > > before we could suggest something that would work in all cases.
> > > >
> > > > If every server would have FTP running on it, then you could, in
> theory,
> > > use
> > > > FTP to send the data file to the server. However, the FTP protocol
is
> > not
> > > a
> > > > lot of fun to implement yourself, so you would probably want to look
> at
> > > one
> > > > of the add-on products for .NET CF which would do that. You should
be
> > > able
> > > > to search for FTP in the Google archives of this newsgroup to get
some
> > > > suggestions.
> > > >
> > > >
> > >
> >
>
http://groups.google.com/groups?hl=en&lr=lang_en&ie=UTF-8&oe=UTF-8&group=microsoft.public.dotnet.framework.compactframework
> > > >
> > > > If the server will be running a Web server in every case, that is
> > another
> > > > possibility. The .NET CF's own support for HTTP:/HTML is closer to
> > > allowing
> > > > you to do this yourself, or, again, you could use the help of an
> add-on
> > > > product for file upload.
> > > >
> > > > If the servers are all Windows-based, you might be able to map a
> shared
> > > > network 'drive' for use by the Windows CE device. I don't recall
> there
> > > > being any direct support in the Compact Framework for this, but I
> could
> > be
> > > > wrong about that. Once the share was mapped, you could use the
> standard
> > > > file I/O stuff in .NET CF for the actual file operations (you simply
> > > create
> > > > the file in a particular location (something like \network\share
name
> on
> > > > computername\desiredfilename).
> > > >
> > > > Paul T.
> > > >
> > > > "Robert J" <paa@bigpond.com> wrote in message
> > > > news:mYvBc.47507$sj4.5210@news-server.bigpond.net.au...
> > > > > Hi,
> > > > >
> > > > > I need to be able to write a text file and save it to a directory
on
> a
> > > > > Server
> > > > > from in my Mobile Device program using VB.Net.
> > > > >
> > > > > I can do this by calling a Web Service, but all the Servers on the
> > sites
> > > > > where my app is deployed can not, or will not, be running the .Net
> > > > > framework.
> > > > >
> > > > > What other options do I have FTP using sockets (almost sounded
like,
> > > > > I knew what I was talking about there) this would all be new to
me,
> > have
> > > > > always used the winsock wrapper in VB 6 in the past.
> > > > >
> > > > > Can you can access a network folder using the System.IO.Filestream
> by
> > > > > \\Servername\C:\Folder ?
> > > > >
> > > > > When I try to connect in this way from my Pocket Pc Device it
always
> > > > > asks for the Username and Passwords, is this related to
permissions,
> > > > > and can they be set for the mobile devices.
> > > > >
> > > > > Still finding my way with .Net and the Compact Framework, so let
me
> > > > > know if I am barking up the wrong bush.
> > > > >
> > > > > Cheers
> > > > >
> > > > > Robert
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>