Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Stupid "DirectoryNotFoundException"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve Wolfie - 05 Aug 2005 21:22 GMT
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
Steve Wolfie - 05 Aug 2005 23:59 GMT
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

> can anyone please tell me why this throws "DirectoryNotFoundException" it
> also does not set the attributes like I am requesting.
[quoted text clipped - 43 lines]
>
> End Module
Lloyd Dupont - 06 Aug 2005 01:43 GMT
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..."

Signature

There are 10 kinds of people in this world. Those who understand binary and
those who don't.

> can anyone please tell me why this throws "DirectoryNotFoundException" it
> also does not set the attributes like I am requesting.
[quoted text clipped - 45 lines]
>
> End Module
Steve Wolfie - 07 Aug 2005 20:35 GMT
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
> I wonder.. is VB strongly typed or not?
>
[quoted text clipped - 60 lines]
> >
> > End Module
Hector Obregon [eMVP] - 06 Aug 2005 01:46 GMT
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

> can anyone please tell me why this throws "DirectoryNotFoundException" it
> also does not set the attributes like I am requesting.
[quoted text clipped - 45 lines]
>
> End Module
Steve Wolfie - 07 Aug 2005 20:35 GMT
Hmmmm.... so to fix it?

Steve
> Most likely your code lacks enough permissions to access that
> directory/file. This will sometimes throw the exception you see instead of a
[quoted text clipped - 53 lines]
> >
> > End Module
"Peter Huang" [MSFT] - 08 Aug 2005 03:50 GMT
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

Signature

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

Steve Wolfie - 09 Aug 2005 00:34 GMT
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
>
[quoted text clipped - 36 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 09 Aug 2005 03:43 GMT
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

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.