|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help using Right() string function?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 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 I've compiled a DLL which is being called from a different project. Thenews:%23q3eYNdOFHA.2468@tk2msftngp13.phx.gbl... 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 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 Show quoteHide quote "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message news:ek7bxYdOFHA.3828@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:%23q3eYNdOFHA.2468@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 > > > > > Brett,
In addition to Ken's comments: | If Microsoft.VisualBasic.Right(FilePath, 1) = "\" Then It appears that you are appending a "\" if the path already ends in a "\", I | FilePath = FilePath & "\" | End If | txtfile = fso.OpenTextFile(FilePath & FileName, 2, True) 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 Dim filePathName As String| String, ByVal FileContent As String) | Dim txtfile As Object | Dim fso = CreateObject("Scripting.FileSystemObject") filePathName = Path.Combine(filePath, fileName) | txtfile = fso.OpenTextFile(filePathName, 2, True) Also rather then user Right(x, 1) you can use String.EndsWith("\").| txtfile.WriteLine(FileContent) | txtfile.WriteLine() | End Sub | If FilePath.EndsWith("\") Then Hope this helps| FilePath = FilePath & "\" | End If Jay Show quoteHide quote "Brett" <no@spam.net> wrote in message news:%23q3eYNdOFHA.2468@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 | | | |
datagrid filled from a list
HLP: Retrieving ListView String Information Checking a string for valid date Parameter it doesn't work (for me) II Parameters it doesnt work (for me) Sequential file access and binary file access socket class Datagrid Question File Access Parameter it doesn't work (for me) IIII |
|||||||||||||||||||||||