How do you programatically put a file on the desktop?
Thanks
Thanks
' All below here is copied and converted from the link. - GHL ''================================================== ' 'found here: [URL="http://www.devbuzz.com/Archived/zinc_eVB_copy_files_to_device_II_pg2.aspx"]'http://www.devbuzz.com/Archived/zinc_eVB_copy_files_to_device_II_pg2.aspx[/URL] ' '================================================== 'This first Function first calls CeFindFirstFile To Check If the file 'which it Is To Write To On the Pocket PC exists. If so, the routine 'will Exit. It would be simple enough To modify this To prompt For 'overwrite etc. The Next action performed Is To Call CeCreateFile. 'This Function has a multitude Of uses And Is used both To Read files 'From the device, And Write To them. The best way I can describe it 'really Is Not CreateFile but 'Create me a File Handle for this file 'With these settings'. We are using GENERIC_WRITE which means writing 'To the file, FILE_SHARE_READ which indicates no-one Else can Open 'this file For writing whilst we are accessing it, CREATE_ALWAYS which 'means Create the file no-matter what, And FILE_ATTRIBUTES_NORMAL 'which means we don't wish to set any system or read-only type 'attributes On the file. Next we use the ReadFileAsBinary Function '(which Is a little further On) To Read the Client file From the PC 'into a Byte Array. This buffer Of Bytes Is Then passed into the 'CeWriteFile routine, along With the file Handle given To us by the 'CeCreateFile Function, the number Of bytes In the buffer, And a 'variable To receive how many bytes have been written by the Call. ' 'If we receive a non-zero value From the Call, it has been a success. 'Public Function CopyFileFromPocketPC() As Boolean Function CopyFileFromPocketPC() As Long Dim bytBuffer(16384) As Byte Dim lngFileHandle As Long Dim lngBytesRead As Long Dim typFindFileData As CE_FIND_DATA Dim intFreeFileID As Integer Dim intWriteLoop As Integer 'locate the file, and see if it already exists on the device lngFileHandle = CeFindFirstFile(Form1.txtPPCFile.Text, typFindFileData) If lngFileHandle = INVALID_HANDLE Then MsgBox "File " & Form1.txtPPCFile.Text & " Not Found. Operation Aborted.", vbOKOnly CopyFileFromPocketPC = False Exit Function End If 'we dont need this handle now that we know the file is there CeFindClose lngFileHandle 'i know it seems odd to have to call a function called CreateFile to read a file 'but what it really refers to is create me a handle to a file of this type. lngFileHandle = CeCreateFile(Form1.txtPPCFile.Text, GENERIC_READ, FILE_SHARE_READ, vbNullString, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) If lngFileHandle = INVALID_HANDLE Then MsgBox "Failed to open file " & Form1.txtPPCFile.Text CopyFileFromPocketPC = False Exit Function End If intRetVal = CeReadFile(lngFileHandle, bytBuffer(0), 16384, lngBytesRead, 0) 'if we got a 0 return value from readfile then there is an error 'so pass it to the error handler If intRetVal = READ_ERROR Then GoTo ErrHandler End If intFreeFileID = FreeFile Open Form1.txtDesktopFile.Text For Binary As intFreeFileID For intWriteLoop = 0 To lngBytesRead Put #intFreeFileID, intWriteLoop + 1, bytBuffer(intWriteLoop) Next intWriteLoop Close intFreeFileID CopyFileFromPocketPC = True Exit Function ErrHandler: MsgBox "Error " & CeGetLastError & " Occurred When Copying File " & _ Form1.txtPPCFile.Text & " From The Device as " & Form1.txtDesktopFile.Text, vbCritical & vbOKOnly CopyFileFromPocketPC = False End Function '============================================= 'The second Function starts Off In a very similar fashion To the first '(I did think about making the Call To CeFindFirstFile a separate 'Function Call To tidy the Code up, but I have To leave you guys some 'things To play With), With a Call To CeFindFirstFile. After All, If 'the file that we are trying To Read doesnt exist, there really isnt 'Any point trying To Read it. Then again we Call CeCreateFile. This 'time we are Using GENERIC_READ As we are reading, FILE_SHARE_READ 'which indicates no-one Else can Open this file For writing whilst we 'are accessing it, OPEN_EXISTING To retrieve a file Handle To Open an 'existing file, And FILE_ATTRIBUTES_NORMAL which means we don't wish 'To Set Any system Or Read-Only Type attributes On the file. Next we 'use the CeReadFile Function To retrieve a maximum Of 16384 bytes, 'From the file Handle provided by CeCreateFile, into another Array Of 'bytes. This Byte buffer Is filled With bytes From the file On the CE 'device, And the number Of Bytes Read And placed In the buffer Is 'written into lngBytesRead. ' 'If we receive a non-zero value From the Call, it has been a success, 'so we can carry On And Write the file To the pc. ' 'We now use a simple piece Of Visual basic To Create a New file On the 'Local pc, opened In Binary Mode, And Loop through the Byte buffer, 'writing Out the contents, Byte by Byte, into the PC file. '============================================= '============================================= 'Private Function ReadFileAsBinary(strSrcFilename As String, lngFileSize As Long, bytBuffer() As Byte) As Boolean Function ReadFileAsBinary(strSrcFilename As String, lngFileSize As Long, bytBuffer() As Byte) As Long Dim intFileHandle As Integer Dim intSeekPos As Integer 'On Error GoTo ErrHandler lngFileSize = FileLen(strSrcFilename) intFileHandle = FreeFile Open strSrcFilename For Binary As intFileHandle For intSeekPos = 1 To lngFileSize Get #intFileHandle, intSeekPos, bytBuffer(intSeekPos - 1) Next intSeekPos Close intFileHandle ReadFileAsBinary = True Exit Function ErrHandler: MsgBox "Error reading file " & strSrcFilename, vbCritical & vbOKOnly ReadFileAsBinary = False End Function '================================================== '
'PBWIN 9.00 - WinApi 05/2008 - XP Pro SP3 #Compile Exe #Dim All #Include "WIN32API.INC" #Include "COMDLG32.INC" Function PBMain ErrClear Dim strPath As Asciiz * %MAX_PATH, r& r = SHGetFolderPath(0, %CSIDL_DESKTOPDIRECTORY, 0, 0, strPath) ' Need to change below appropriately FileCopy "c:\Temp\Test.txt" , strpath & "\Test1.txt" ' ? Error$(Err) & Str$(Err),, strpath & "\Test1.txt" End Function '
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment