Solved

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

Posted on 2011-03-25
8
1,083 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:RobertoFreemano
Comment Utility
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
Comment Utility
That would be something like this:

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

Author Comment

by:RobertoFreemano
Comment Utility
Hi TLO,

I'll test this now ;)

Sorry about the delay :(
0
 

Author Closing Comment

by:RobertoFreemano
Comment Utility
Thanks ;)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now