Visual Studio 2012
VB.NET Windows forms application
.NET Framework 4.5.2
VB has finally won. I am out of ideas.
The File.WriteAllText function throws the exception "Could not find file" even though MS says that it can only throw the following exceptions (MS documentation):
' Exceptions: ' T:System.ArgumentException: ' path is a zero-length string, contains only white space, or >contains one or more ' invalid characters as defined by >System.IO.Path.InvalidPathChars. ' ' T:System.ArgumentNullException: ' path is null or contents is empty. ' ' T:System.IO.PathTooLongException: ' The specified path, file name, or both exceed the system-> defined maximum length. ' For example, on Windows-based platforms, paths must be less > than 248 characters, ' and file names must be less than 260 characters. ' ' T:System.IO.DirectoryNotFoundException: ' The specified path is invalid (for example, it is on an unmapped drive). ' ' T:System.IO.IOException: ' An I/O error occurred while opening the file. ' ' T:System.UnauthorizedAccessException: ' path specified a file that is read-only.-or- This operation is not supported ' on the current platform.-or- path specified a directory.-or- The caller does ' not have the required permission. ' ' T:System.NotSupportedException: ' path is in an invalid format. ' ' T:System.Security.SecurityException: ' The caller does not have the required permission.
The code (it worked last week):
Dim filepath As String = StringFilePath + "\" + DateTime.Now.ToString("yyyy/MM")
Dim stringFile As String = ""
If My.Computer.FileSystem.DirectoryExists(filepath) = False Then
My.Computer.FileSystem.CreateDirectory(filepath)
End If
stringFile = StringHTMLHeader + StringTestDoc + StringHTMLFooter
StringLastSavedFile = Path.Combine(filepath, "Test Result " + DateTime.Now.ToString("dd-HH-mm-ss") + ".htm")
Try
File.WriteAllText(StringLastSavedFile, stringFile)
Catch ex As Exception
End Try
- Checking the filepath etc. etc.
- Running as admin
- And, of course, restarting.
I tried the following code and it worked. I had to guess at some of the variables based on the various comments.