By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,022 Members | 1,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,022 IT Pros & Developers. It's quick & easy.

Help using Right() string function?

P: n/a
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


Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
---------------------
"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:ek**************@TK2MSFTNGP10.phx.gbl...
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
---------------------
"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 21 '05 #3

P: n/a
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
"Brett" <no@spam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| 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
|
|
|
|
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.