My Assistant
![]()
Custom Search
|
![]() ![]() |
![]() |
![]() Post#1 | |
Posts: 7,115 Joined: 22-December 10 From: England ![]() | Hi, I'm working on my first vb .net programme. A backup utility featuring recursive calls. It is all working fine until it comes to a file that is locked by another process. The line that errors is this CODE FromFile.CopyTo(ToFileName, True) I get an exception thrown "System.IO.IOException: 'The process cannot access the file because another process has locked a portion of the file." With VBA I could trap such an error and ask the code to continue to the next line, but am struggling with how to do that in VB .Net. It look like I need a Try/Catch and Finally bit of code, but can't work out how to do it. All help appreciated. Thanks David |
![]() Post#2 | |
![]() UtterAccess Administrator Posts: 10,274 Joined: 7-December 09 From: St. Augustine, FL ![]() | I don't think you can do that in .NET. A try/catch is typically how you'd handle exceptions, but once you get to the catch block, I don't know that you can return to the next line of execution in the try block. Why would you want to? If the file is locked, why continue? Alternatively, check of a file lock prior to trying to execute IO on it. There's still a potential race condition, but at least then you can use a simple If statement to bypass it (if you feel you really need to). -------------------- |
![]() Post#3 | |
Posts: 7,115 Joined: 22-December 10 From: England ![]() | Hi folks: FWIW I found A solution, which works - I need to refine it a bit,but any comments would be appreciated CODE Try FromFile.CopyTo(ToFileName, True) Catch ex As System.IO.IOException If ex.HResult = -2147024863 Or ex.HResult = -2147024864 Then ' these are the two exceptions found sofar Else MsgBox(ex.HResult & " - " & ex.Message) End If End Try I think I just need to put "Return" after the message box - But I will do that later. |
![]() Post#4 | |
![]() UtterAccess VIP Posts: 11,255 Joined: 10-February 04 From: South Charleston, WV ![]() | Try this. When I tried to find documention on a .CopyTo method what I got was a string operation. So where did you get this? How do you specify the source file? -------------------- Robert Crouser |
![]() Post#5 | |
Posts: 7,115 Joined: 22-December 10 From: England ![]() | Thanks Jack: In this case I only have the one line of code in the try statement, and I discovered that the default actions is in effect 'resume next' in VBA speak. I will also look at catching the fact that the file is locked. It's all good learning David |
![]() Post#6 | |
Posts: 7,115 Joined: 22-December 10 From: England ![]() | Hi Robert: I pick up the source and destination folder for a couple of text boxes on a form, together with some Booleand covering what is to be copied, and if subfolders are wanted. Here is the whole sub that does the work CODE Private Sub CopyNow(FromFolder As String, ToFolder As String, Newer As Boolean, NotThere As Boolean, CopyAll As Boolean, AndSubs As Boolean, ByRef Examined As Long, ByRef Copied As Long) Dim FromFile As FileInfo Dim fldrSource As DirectoryInfo Dim OKtoCopy As Boolean Dim ToFileExists As Boolean Dim ToFileName As String fldrSource = FileIO.FileSystem.GetDirectoryInfo(FromFolder) For Each FromFile In fldrSource.GetFiles ToFileName = ToFolder & "\" & FromFile.Name ToFileExists = IO.File.Exists(ToFileName) Examined = Examined + 1 If NotThere = True And ToFileExists = False Then OKtoCopy = True ElseIf CopyAll = True Then OKtoCopy = True ElseIf Newer = True Then If ToFileExists Then If FromFile.LastWriteTime > IO.File.GetLastWriteTime(ToFileName) Then OKtoCopy = True Else OKtoCopy = False End If Else 'tofile doesn't exist OKtoCopy = True End If Else OKtoCopy = False End If If OKtoCopy = True Then If Directory.Exists(ToFolder) Then Else Directory.CreateDirectory(ToFolder) End If Try Copied = Copied + 1 FromFile.CopyTo(ToFileName, True) Catch ex As System.IO.IOException If ex.HResult = -2147024863 Or ex.HResult = -2147024864 Then Copied = Copied - 1 Else MsgBox(ex.HResult & " - " & ex.Message) End If End Try End If Next If AndSubs = True Then For Each fldrSource In fldrSource.GetDirectories FromFolder = fldrSource.FullName ToFolder = Replace(FromFolder, Me.txtCopyFrom.Text, txtCopyTo.Text) Call CopyNow(FromFolder, ToFolder, Newer, NotThere, CopyAll, AndSubs, Examined, Copied) Next End If End Sub Happy to explain any of the steps, and happy to receive any comments. (Please be gentle with me, this is my first ever vb .net piece of work) |
![]() Post#7 | |
![]() UtterAccess VIP Posts: 11,255 Joined: 10-February 04 From: South Charleston, WV ![]() | This is your first vb.net program? My reaction is it's quite complex! I don't believe I've ever done such a file manipulation program. In any language. This type of file manipulation makes me think either you're programming a utility or there is a design issue. Or maybe neither. -------------------- Robert Crouser |
![]() Post#8 | |
Posts: 7,115 Joined: 22-December 10 From: England ![]() | Hi Robert: Yes it IS intended as a utility. I've written similar ones using Excel and Access as the 'holder' of the user interface, and it seemed like a reasonable start for VB.net. (I didn't see much point in a "Hello World") So I knew more or less how I wanted it to go, and it was in effect a 'translation' exercise -- and wasn't easy !!.. Very simple requirement To back up files from one place to another, using a simple copy process. User to select source and destination folders User to chose Copy all Copy only Newer Copy only those missing Whether subfolders are wanted. Program to display at the end how many files were looked at and how many were copied. Next problem: How to turn that into an exe file. Thanks for the input David |
![]() Post#9 | |
![]() UtterAccess VIP Posts: 11,255 Joined: 10-February 04 From: South Charleston, WV ![]() | You're welcome. Not sure you can do that with vb. -------------------- Robert Crouser |
![]() Post#10 | |
![]() UtterAccess Administrator Posts: 10,274 Joined: 7-December 09 From: St. Augustine, FL ![]() | >> Next problem: How to turn that into an exe file << Set the Project Type as a Console Application (right click the project and select Properties, or in the VS menu: Project -> Properties - it'll be on the Application tab in the properties). When you change it to a Console App, it'll need a static main void entry point. I don't work with VB.NET (we use C#), so I can't give the exact syntax, but I'm sure you can find it online. The C# equivalent will be: CODE public static void main(string[] args) ... in VB you'll have the same in a slightly different syntax. The single argument, "args" will be an array of strings, which will hold your commandline parameters (split by space, but you can use quotations to delimit a value with spaces in). hth -------------------- |
![]() Post#11 | |
Posts: 7,115 Joined: 22-December 10 From: England ![]() | As a final update, I did manage to get an installation package with a Setup.exe to install it all as an application. Can't remember exactly how I got there in then end, but the final step was an option to "Publish" the application. (And it was a lot easier than doing it with Access and the runtime). I now have my backup utility available from my desktop.. Thanks for all the pointers David |
![]() Post#12 | |
![]() Posts: 337 Joined: 21-September 14 From: Tampa Bay, Florida, USA ![]() | Including these at the begining and refactoring your code would simplify your console program. CODE Imports Microsoft.VisualBasic.FileIO Imports System.Environment Microsoft.VisualBasic.FileIO Namespace Properties and methods for working with drives, files, and directories, e.g. My.Computer.FileSystem Environment Class For retrieving information such as command-line args, exit codes, environment variable settings, call stack contents, time since last system boot, CLR version. This post has been edited by FrankRuperto: Nov 11 2019, 10:57 AM -------------------- Currently supporting pawnbrokers that use my store management system developed with Access 2010 on Windows7. Experienced with Informix and Oracle DB's. |
![]()
Custom Search
|
![]() | Search Top Lo-Fi | 8th December 2019 - 09:04 PM |