Stupid "DirectoryNotFoundException"

  • Thread starter Thread starter Steve Wolfie
  • Start date Start date
S

Steve Wolfie

can anyone please tell me why this throws "DirectoryNotFoundException" it
also does not set the attributes like I am requesting.

Thanks, I am still pretty novice. I thought this would be pretty simple.....

Steve

Imports System.IO
Module Module1

Sub Main()
Dim ofile as file
Dim odir as directory
dim owriter as streamwriter
dim dirlist1 as string() = odir.getdirectories("C:\documents and
settings\user\my documents")
dim list as string
dim str as string
dim i as integer
dim chararr as string()
dim attrib as fileattributes
dim array as string()

For each list in dirlist1
array = odir.getfiles(list)
For each str in array
attrib = ofile.getattributes(str)
If attrib = 32 then ' this tells me if it is
ready for archiving
chararr = str.split("\") ' splits the
C:\dir\dir\dir\dir\filename.ext into an array
i = chararr.length - 1 'selects only
the filename from the array
ofile.copy(str, "C:\backupdir\" &
list.replace("C:\documents and settings\user\","") & "\" & charrarr(i)) '
this line throws the exception..... can't figure out why...
ofile.setattributes(str,
fileattributes.normal) ' this line does not set the attribute like i want
Else
End If
Next
Next
Console.writeline("Finished.")
console.readline()
End Sub

End Module
 
May I add, that I am attempting to write a *simple* backup program to
recurse through 2 separate directories and only backup files that are marked
ready to archive and then mark those copied files as ready for archiving...

Steve
 
I wonder.. is VB strongly typed or not?

Anyway with the exception information you should also get the line at which
the exception occured.
Hence you could determine the statement which fails.

Just an additional idea, were you writting in C#, C, C++, Java and/or
ObjectiveC this string:
"C:\do..." would be incorrect.
I suppose it might be the same in VB.

better use either:
- "C:\\do..."
- or "C:/do..."
 
Most likely your code lacks enough permissions to access that
directory/file. This will sometimes throw the exception you see instead of a
clearer "AccessDenied" kind of message.

Cheers,

Hector
 
Thanks for the input, but, no the correct syntax is "C:\path" Someone else
proposes permissions, but that is not the case either.

Really confusing.

Steve
 
Hmmmm.... so to fix it?

Steve
Hector Obregon said:
Most likely your code lacks enough permissions to access that
directory/file. This will sometimes throw the exception you see instead of a
clearer "AccessDenied" kind of message.

Cheers,

Hector
 
Hi

attrib = ofile.getattributes(str)
If attrib = 32 then ' this tells me if it is
ready for archiving
chararr = str.split("\") ' splits the
C:\dir\dir\dir\dir\filename.ext into an array
i = chararr.length - 1 'selects only
the filename from the array

'Try to extract the destination string first
e.g.
dim path as string = "C:\backupdir\" & list.replace("C:\documents and
settings\user\","") & "\" & charrarr(i)
'check the path manually to see if the path is valid
'e.g. path = "C:\dir1\dir2\filename.ext", try to check if the dir1,dir2
existed
ofile.copy(str,path)



ofile.copy(str, "C:\backupdir\" &
list.replace("C:\documents and settings\user\","") & "\" & charrarr(i)) '
this line throws the exception..... can't figure out why...

ofile.setattributes(str,
fileattributes.normal) ' this line does not set the attribute like i want
'I think the code line before is not executed if the ofile copy have
exception occurred.

Else
End If

Hope this helps.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for the input Peter.

I actualy started really digging into it using the debugger and stepping
into and out of code block and you will never believe the outcome.

The program *deleted* the file. I am not kidding, and would be happy to
reproduce the anomaly. When the attibute is applied, the file disappears
from any view (dir, explorer) it is not hidden and could not be found with
winternals fileRecover utiility. I amazed myself and my boss several times,
erasing files by changing the attribute. When in break mode, the program
never threw the exception. (maybe that is because this is the first time
that I have ever used the debugger properly.) Can you shed any light into
this?

How is this possible?

Steve
 
Hi

I can reproduce the scenario with your code.
ofile.Copy will throw exception on my side because of the destination path
is not existing.


Here is my test code which works file on my side.
I add the code line below to ensure the destination path existed.
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If

You may have a try.

Imports System.IO
Module Module1
Sub Main()
Dim ofile As File
Dim odir As Directory
Dim owriter As StreamWriter
Dim dirlist1 As String() = odir.GetDirectories("C:\Documents and
Settings\user\My Documents")
Dim list As String
Dim str As String
Dim i As Integer
Dim chararr As String()
Dim attrib As FileAttributes
Dim array As String()

For Each list In dirlist1
array = odir.GetFiles(list)
For Each str In array
attrib = ofile.GetAttributes(str)
If attrib = 32 Then
chararr = str.Split("\")
i = chararr.Length - 1
Dim path As String = "C:\backupdir\" &
list.Replace("C:\Documents and Settings\user\", "")
Dim filepath As String = path & "\" & chararr(i)
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
Debug.WriteLine("Copy from " & str & " to " & filepath)
ofile.Copy(str, filepath)
ofile.SetAttributes(str, FileAttributes.Normal)
Else
End If
Next
Next
Console.WriteLine("Finished.")
Console.ReadLine()
End Sub
End Module


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top