Help using Right() string function?

B

Brett

I've compiled a DLL which is being called from a different project. The
following code is from the DLL. When I run the WriteToFile sub the second
time from the spawning project, I get the error that follows.


Imports Microsoft.VisualBasic

Public Shared Sub WriteToFile(ByVal FilePath As String, ByVal FileName As
String, ByVal FileContent As String)
Dim txtfile As Object
Dim fso = CreateObject("Scripting.FileSystemObject")
If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then
FilePath = FilePath & "\"
End If
txtfile = fso.OpenTextFile(FilePath & FileName, 2, True)
txtfile.WriteLine(FileContent)
txtfile.WriteLine()
End Sub

The following error occurs on this line:
If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then

Error is:

An unhandled exception of type 'System.Security.SecurityException' occurred
in microsoft.visualbasic.dll

Additional information: Exception from HRESULT: 0x800A0046
(CTL_E_PERMISSIONDENIED).

Any suggestions what the error means or why it only occurs on the second
call to this method?

Thanks,
Brett
 
K

Ken Tucker [MVP]

Hi,

There are a lot of errors in your code. First dont use late
binding it causes a lot of errors that are hard to find. Second your code
will run quicker if you use native dot net classes. Third always close a
file that you create. Try something like this instead

Public Shared Sub WriteToFile(ByVal FilePath As String, ByVal FileName As
String, ByVal FileContent As String)

If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then

FilePath = FilePath & "\"

End If

Dim sw As New IO.StreamWriter(FilePath & FileName)

sw.WriteLine(FileContent)

sw.WriteLine()

sw.Close()

End Sub



Ken
---------------------
I've compiled a DLL which is being called from a different project. The
following code is from the DLL. When I run the WriteToFile sub the second
time from the spawning project, I get the error that follows.


Imports Microsoft.VisualBasic

Public Shared Sub WriteToFile(ByVal FilePath As String, ByVal FileName As
String, ByVal FileContent As String)
Dim txtfile As Object
Dim fso = CreateObject("Scripting.FileSystemObject")
If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then
FilePath = FilePath & "\"
End If
txtfile = fso.OpenTextFile(FilePath & FileName, 2, True)
txtfile.WriteLine(FileContent)
txtfile.WriteLine()
End Sub

The following error occurs on this line:
If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then

Error is:

An unhandled exception of type 'System.Security.SecurityException' occurred
in microsoft.visualbasic.dll

Additional information: Exception from HRESULT: 0x800A0046
(CTL_E_PERMISSIONDENIED).

Any suggestions what the error means or why it only occurs on the second
call to this method?

Thanks,
Brett
 
B

Brett

Thanks Ken. The tips are great and the code is working really well now.
The "sw" stream object doesn't have a dispose method. Should I do

sw = nothing

to destroy it?

Also, in the code below, the StreamWriter method seems fine with a file path
that has no trailing "\". It writes either way. Is the trailing slash
optional?

When I use this DLL in projects and compile the project EXE, it seems the
EXE will not work without the DLL. This means the application requires two
files. What is the best method for distributing them if a user already has
the .NET Framework installed? I was thinking of a Winzip EXE.

Thanks,
Brett
 
J

Jay B. Harlow [MVP - Outlook]

Brett,
In addition to Ken's comments:

| If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then
| FilePath = FilePath & "\"
| End If
| txtfile = fso.OpenTextFile(FilePath & FileName, 2, True)

It appears that you are appending a "\" if the path already ends in a "\", I
would expect you only want to append a "\" if the path does not end in a
"\"! Correct? Otherwise you are doubling the last "\"!

Rather then build the full path yourself, I would recommend you use
System.IO.Path.Combine. As it avoids the above subtle bug...

Something like:

Imports System.IO
| Public Shared Sub WriteToFile(ByVal FilePath As String, ByVal FileName As
| String, ByVal FileContent As String)
| Dim txtfile As Object
| Dim fso = CreateObject("Scripting.FileSystemObject")

Dim filePathName As String
filePathName = Path.Combine(filePath, fileName)

| txtfile = fso.OpenTextFile(filePathName, 2, True)
| txtfile.WriteLine(FileContent)
| txtfile.WriteLine()
| End Sub


Also rather then user Right(x, 1) you can use String.EndsWith("\").

| If FilePath.EndsWith("\") Then
| FilePath = FilePath & "\"
| End If

Hope this helps
Jay


| I've compiled a DLL which is being called from a different project. The
| following code is from the DLL. When I run the WriteToFile sub the second
| time from the spawning project, I get the error that follows.
|
|
| Imports Microsoft.VisualBasic
|
| Public Shared Sub WriteToFile(ByVal FilePath As String, ByVal FileName As
| String, ByVal FileContent As String)
| Dim txtfile As Object
| Dim fso = CreateObject("Scripting.FileSystemObject")
| If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then
| FilePath = FilePath & "\"
| End If
| txtfile = fso.OpenTextFile(FilePath & FileName, 2, True)
| txtfile.WriteLine(FileContent)
| txtfile.WriteLine()
| End Sub
|
| The following error occurs on this line:
| If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then
|
| Error is:
|
| An unhandled exception of type 'System.Security.SecurityException'
occurred
| in microsoft.visualbasic.dll
|
| Additional information: Exception from HRESULT: 0x800A0046
| (CTL_E_PERMISSIONDENIED).
|
| Any suggestions what the error means or why it only occurs on the second
| call to this method?
|
| Thanks,
| Brett
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top