Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File IO Problem

Status
Not open for further replies.

DarkConsultant

Programmer
Joined
Dec 4, 2007
Messages
156
Location
GB
Hi Everyone,

This should be my last question before I get my eyes done (cataracts) so please wish me well and tell me why ....

I have an app that uses a common, network folder for logging. All clients connect to the shared folder via Mapped Network Drive letters and save logging to a file for example 'F:\Logs\2009\12\01.txt'.

When a log entry is written ...

Public Sub WriteLogEntry(ByVal LogEntry as String)

Dim MyLogFile as String = "F:\Logs\2009\12\01.txt"

Try

Try

If Directory.Exists(Path.GetDirectoryName(MyLogFile)) = False Then

Directory.Create(Path.GetDirectoryName(MyLogFile))

End If

Catch ex As Exception
1 * FAILS HERE
End Try

Dim Exists As Boolean = File.Exists(MyLogFile)

Using LogFile As New StreamWriter(MyLogFile,Exists)

LogFile.WriteLine(LogEntry)

End Using

Catch ex As Exception
2 * FAILS HERE
End Try

End Sub

The code above fails at fail point 1 on around 15% of clients with the error message

System.IO.DirectoryNotFoundException: Could not find a part of the path \'F:\\CCTraining\\System\\Logs\\2009\\12\'.

and at fail point 2

System.IO.DirectoryNotFoundException: Could not find a part of the path \'F:\\CCTraining\\System\\Logs\\2009\\12\\01 Log.txt\'.

AND the file does not get written to.

85% of clients are fine and never error.

I have searched on Google and here but my eyes prevent me from reading most of it and so I cannot resolve the problem.

Many thanks

DarkConsultant

Live long and prosper \\//
 

Well, you have this:

Dim MyLogFile as String = "F:\Logs\2009\12\01.txt"

But the error has a different path:

System.IO.DirectoryNotFoundException: Could not find a part of the path \'F:\\CCTraining\\System\\Logs\\2009\\12\\01 Log.txt\'.

Is that the actual error message, complete with double backslashes?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hello jebensen,

Sorry mate I had to retype it as I couldn't copy & paste and some typos crept in.

The second path is correct (the cctraining one).

I omitted to say before but the path to the log file exists and should not need to be created.

If I halt execution before the first error the log file shows as F:\CCTraining\System\Logs\2009\12\01 Log.txt but acquires doubles slashes when erroring. I thought this was something to do with it being a mapped network drive (F:) but cant seem to find out.

If I connect via UNC the error does not happen but my clients like mapped network drives.

I was informed that putting "\?\\" in front of the path would solve the problem but it didnt.

Any ideas?

I have reposted the code ....

Public Sub WriteLogEntry(ByVal LogEntry as String)

Dim MyLogFile as String = "F:\CCTraining\System\Logs\2009\12\01.txt"

Try

Try

If Directory.Exists(Path.GetDirectoryName(MyLogFile)) = False Then

Directory.Create(Path.GetDirectoryName(MyLogFile))

End If

Catch ex As Exception
1 * FAILS HERE
End Try

Dim Exists As Boolean = File.Exists(MyLogFile)

Using LogFile As New StreamWriter(MyLogFile,Exists)

LogFile.WriteLine(LogEntry)

End Using

Catch ex As Exception
2 * FAILS HERE
End Try

End Sub

DarkConsultant

Live long and prosper \\//
 
First, it should be .CreateDirectory not .Create in all versions of vb.net so I assume it is a typo? Other than that the code is correct then the only other suggestion is to get rid of the trys and find out what line it actually errors on. It is possible it is a computer/network issue, path mapping wrong in some way, or something like that. If everything checks out then I don't know what to tell you. 90% of my work is done across a LAN to remote drives/systems. I usually use mapped drives (because IT likes to move drives/paths around without notice) and I've never had a problem like that.

A few other things. I wouldn't use nested trys like that. There is no point in going further if the directory is needed and it fails to create it. Another is while there is nothing wrong with doing it there is no reason to check if the file Exists if you are just going to append to it

Code:
    Public Sub WriteLogEntry(ByVal LogEntry As String)
        Dim MyLogFile As String = "F:\CCTraining\System\Logs\2009\12\01.txt"

        Try
            If Directory.Exists(Path.GetDirectoryName(MyLogFile)) = False Then
                Directory.CreateDirectory(Path.GetDirectoryName(MyLogFile))
            End If

            Using LogFile As New StreamWriter(MyLogFile, True)
                LogFile.WriteLine(LogEntry)
            End Using
        Catch ex As Exception
            'Do something on Exception?
        End Try
    End Sub

After thinking about it again I tried removing the CreateDirectory and the StreamWriter will throw that very error if it can't find the directory so that seems likely that you are using .Create instead and that is what the problem is on the second error at least. Though I can't think why you would only get the can't find part of the path on the first.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Are the "clients" on your network? If so, I would also suggest using "\\servername\c$\CCTraining\System\Logs\2009\12\01.txt" rather than an Explicitly mapped drive letter. Although most will use "F" as their drive letter, some may use a different drive letter which would cause your CreateDirectory method to fail. And assuming the server is in your network, then the above should work, assuming permissions are handled.

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Hi All,

Thanks for the input.

I have a partial fix in not using Mapped Network Drives but my customers prefer them so .....

I have two of the faulting PCs accessible and have just explicitly set the IO permissions on the MND's and the problem seems to be gone EXCEPT when the log file has an underscore in the name e.g. 'F:\Smiths_Books\System\Logs\2009\12\02 Log.txt'

I am down to 4 PC's and a small upgrade (removing the underscores) should solve this.

Thanks to everyone who assisted me with this.

DarkConsultant

Live long and prosper \\//
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top