Solved

copy file from \\network share to c:\... VB.NET 2003\2010 Express

Posted on 2011-03-25
8
1,096 Views
Last Modified: 2012-06-21
Hi Experts,

I want to build a winapp which will list files in a listbox… if the listbox-item is selected and button1 clicked, it will copy/transfer the document from the network repository/folder to local machine (maybe a progress bar to show progress??????? optional).

1.      User opens App
2.      Listbox1 displays e.g. 9 (RTF) files.
3.      user selects Listbox1 item e.g. Outlook
4.      clicks Button1
5.      Outlook - something.rtf copies from network share to c:\dir

The reason I’ve chosen this is because stream-reading RTF docs from a network location can take time to view.

I have code to streamread into Listbox from DIR (which i will add to call, once I get chance).
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String
        Dim di As System.IO.DirectoryInfo

        path = "c:\temp\"
        di = New System.IO.DirectoryInfo(path)

        If (Not di.Exists) Then
            di.Create()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            FileCopy("C:\Documents and Settings\Bob\My Documents\test.txt", "C:\temp")

        Catch ex As Exception
            MsgBox("ok")
        End Try
       
    End Sub

Open in new window

0
Comment
Question by:RobertoFreemano
  • 5
  • 3
8 Comments
 

Author Comment

by:RobertoFreemano
ID: 35221465
If I don't add the TRY code, then I get the following error:

---error--------------------------------------------------------------------------------------------
An unhandled exception of type 'System.IO.IOException' occurred in microsoft.visualbasic.dll

Additional information: The target directory already exists.
------------------------------------------------------------------------------------------------------
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35222058
Are you asking for something more than My.Computer.FileSystem.CopyFile, which has an option for a progress bar, ala Windows Explorer?

File Copy Progress, Custom Thread Pools
http://msdn.microsoft.com/en-us/magazine/cc163851.aspx
0
 

Author Comment

by:RobertoFreemano
ID: 35235154
Sorry TheLearnedOne,

I fond a solution which works but I was wondering if it could incorporate a progress bar?

Thanks,
Roberto
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String
        Dim di As System.IO.DirectoryInfo

        path = "c:\temp\"
        di = New System.IO.DirectoryInfo(path)

        If (Not di.Exists) Then
            di.Create()
        End If

        Dim di2 As System.IO.DirectoryInfo

        path = "c:\temp\temp2"
        di2 = New System.IO.DirectoryInfo(path)

        If (Not di2.Exists) Then
            di2.Create()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim inputStream, outputStream As FileStream
        Dim fileSize As Long
        Dim ipFilebyte As Byte
        inputStream = File.OpenRead("c:\temp\test.txt") 'server path
        fileSize = inputStream.Length()

        outputStream = File.OpenWrite("c:\temp\temp2\test.txt") 'local path

        While fileSize
            ipFilebyte = inputStream.ReadByte()
            outputStream.WriteByte(ipFilebyte)
            fileSize -= 1
        End While

        outputStream.Close()
        inputStream.Close()

    End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 35235271
1) You don't need to use a FileStream to get a progress bar, you can use the FileSystem.CopyFile to copy with a progress bar in 1 line.

2) If you insist on the FileStream approach, then I would suggest using a BackgroundWorker, with progress enabled, and the ReportProgress.
0
 

Author Comment

by:RobertoFreemano
ID: 35235308
Thanks TheLearnedOne,

I'm clueless when it comes to practical coding :(

I wonder if you could assist with "FileSystem.CopyFile "
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35237580
That would be something like this:

      My.Computer.FileSystem.CopyFile(sourceFileName, destinationFileName, FileIO.UIOption.AllDialogs)
0
 

Author Comment

by:RobertoFreemano
ID: 35314307
Hi TLO,

I'll test this now ;)

Sorry about the delay :(
0
 

Author Closing Comment

by:RobertoFreemano
ID: 35386623
Thanks ;)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Open Folder in Windows Explorer 3 95
.NET 2008 VB and C# 6 47
RLDC Reporting in Visual studio 11 42
Name Space error VS2015 1 35
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

685 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