473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help using Right() string function?

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
3 2608
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike | last post by:
Hey guys I am pulling my hair out on this problem!!!!! Any help or ideas or comments on how to make this work I would be grateful! I have been working on this for the past 4 days and nothing I do...
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
1
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h hi iam done with my hw i still have to do one function which is...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.