anonymous user
anonymous user

Reputation: 159

Run Time Error in VB.NET: System.Runtime.InteropServices.COMException

Can anyone pls throw light on this error ?

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in STUB_CREATOR.exe

Additional information: Exception from HRESULT: 0x800A0036 (CTL_E_BADFILEMODE)

This is the line that creates the error

Dim f As New Scripting.FileSystemObject
starter = f.OpenTextFile(stub_path.Text + "\" + cpp_file, Scripting.IOMode.ForWriting)

I have just previously opened the file for writing. but however i have closed the textstream with which i was writing into the file. am i doing anyth worng here ?

Upvotes: 0

Views: 1100

Answers (3)

ThatGuyInIT
ThatGuyInIT

Reputation: 2239

Use the OpenTextFile method to open the text file. The OpenTextFile method requires two parameters: the path to the file and one of the following values:

For reading (parameter value = 1, constant = ForReading). Files opened in this mode can only be read from. To write to the file, you must open it a second time by using either the ForWriting or ForAppending mode.

For writing (parameter value 2, constant = ForWriting). Files opened in this mode will have new data replace any existing data. (That is, existing data will be deleted and the new data added.) Use this method to replace an existing file with a new set of data.

For appending (parameter value 8, constant = ForAppending). Files opened in this mode will have new data appended to the end of the file. Use this method to add data to an existing file.

Basically if you open a file, you have to close it an then reopen it for writing if you want to write to it.

http://technet.microsoft.com/en-us/library/ee198716.aspx

Since you are using VB.NET you, should be using System.IO instead.

Dim mydocpath As String = _
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim sb As New StringBuilder()

For Each txtName As String _
    In Directory.EnumerateFiles(mydocpath, "*.txt")
    Using sr As New StreamReader(txtName)
        sb.AppendLine(txtName.ToString())
        sb.AppendLine("= = = = = =")
        sb.Append(sr.ReadToEnd())
        sb.AppendLine()
        sb.AppendLine()

    End Using
Next

Using outfile As New StreamWriter(mydocpath & "\AllTxtFiles.txt")
    outfile.Write(sb.ToString())
End Using

http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx

Upvotes: 0

Richard
Richard

Reputation: 109130

Is the file writeable? E.g. has the file been closed from the last write or is it still locked (this could easily happen if you are relying on the Garbage Collector to close the file or even release a COM object like FileSystemObject).

Process Monitor can be very helpful to see what the result of the underlying FileOpen is.

One wonders why from VB.net you are using Scripting rather than System.Io? Particularly System.Io.StreamWriter?

Upvotes: 1

Fredrik Mörk
Fredrik Mörk

Reputation: 158379

I can't answer on the specific error, but why are you using Scripting.FileSystemObject? There are so many classes in the .NET framework that will do the job for you. In your case, I would suggest looking into the File.OpenWrite method.

Upvotes: 0

Related Questions