none
System.Security.SecurityException - Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED)

    Question

  • I get the following erroro when at this method.

    test = test2.OpenAsTextStream(Scripting.IOMode.ForWriting)

    where test is Scripting.TextStream and test 2 is  Scripting.File.

    This was just converted to VB.net from vb6. This seems to be running perfactly on VB6 and i get this error only when running it on vb.net. How can this be fixed. ?

    Run-time exception thrown : System.Security.SecurityException - Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED).

    Thanks alot

    Wednesday, November 02, 2005 9:23 PM

Answers

  • Hello,

    There are a few more pieces of information that we need to determine what the exact source of the failure is, including:
     - location where assembly is being executed from [e.g. local machine, internet zone, intranet zone, etc.]
     - the specific security permission that failed (e.g. FileIOPermission).

    My hunch is that the script is being run from a network location, and FileIOPermission is possibly failing.

    To determine the specific problem, try running your script/application under a managed debugger (e.g. mdbg, cordbg, Visual Studio, etc.) - and examine where the application is being loaded from and what specific security permission failed.

    Hope that helps,
    Stephen [Microsoft Common Language Runtime: Security - Developer]
    http://blogs.msdn.com/stfisher
    Monday, November 07, 2005 1:16 AM
    Moderator
  • The CreateTextFile method returns an open TextStream.  Since you never close this TextStream, it's still open when you try to call OpenAsTextStream.  This attempt to open a file that's already locked for writing is what's causing the exception you're seeing.

    You could fix the problem by calling the Close method on the initial TextStream.  However, since the initial call actually opens a TextStream with writing enabled, you could probably use the following as your "else" block:

    m_ts = m_fso.CreateTextFile(m_FileName)

    m_file = m_fso.GetFile(m_FileName)

    Wednesday, November 16, 2005 5:28 PM

All replies

  • Hello,

    There are a few more pieces of information that we need to determine what the exact source of the failure is, including:
     - location where assembly is being executed from [e.g. local machine, internet zone, intranet zone, etc.]
     - the specific security permission that failed (e.g. FileIOPermission).

    My hunch is that the script is being run from a network location, and FileIOPermission is possibly failing.

    To determine the specific problem, try running your script/application under a managed debugger (e.g. mdbg, cordbg, Visual Studio, etc.) - and examine where the application is being loaded from and what specific security permission failed.

    Hope that helps,
    Stephen [Microsoft Common Language Runtime: Security - Developer]
    http://blogs.msdn.com/stfisher
    Monday, November 07, 2005 1:16 AM
    Moderator
  • its being run on a local machine. Also. it is workin properly on vb6 jus the .net translated version is not working properly.

    well,

    If m_fso.FileExists(m_FileName) Then

    m_file = m_fso.GetFile(m_FileName)

    m_ts = m_file.OpenAsTextStream(Scripting.IOMode.ForWriting)

    Else

    m_fso.CreateTextFile(m_FileName)

    m_file = m_fso.GetFile(m_FileName)

    m_ts = m_file.OpenAsTextStream(Scripting.IOMode.ForWriting)

    End If


    thats the code, and if the file FileExists, then it works no problem, but if the file dosent exsist it created the file but throws the error at  "m_ts = m_file.OpenAsTextStream(Scripting.IOMode.ForWriting)". I am workin on a local computer and have all the permissions and loged in as adminstrator. I dont see why it works percatly if the text fil already existe but throws the error if the file doset, even then it creates a blank file with out any problems and throws the error when tring to open it for writing
    Thanks

    Tuesday, November 08, 2005 3:19 PM
  • Have you ensured that the ACLs on the file system allow your program to write to the file in question?

    -Shawn

    Thursday, November 10, 2005 7:06 PM
    Moderator
  • hey, i am not too sure how to do that.

    The streange thing i dont understand is that it works if the file exsist and works properly(writes to the file properly). If the file name dosent exsist it creates the file properly and dosent write to it. Then when i restart the program and point it to the file it just created then it writes to that file properly.

    i dont understand why its creating the file properly but just not writing to it. But when i restart the program and point it to the exact same file it just created it writes properly to it.

    Thanks for your help
    Friday, November 11, 2005 11:25 PM
  • This doesn't sound like a CLR problem as much as it sounds like a problem you're hitting with Windows.  Have you tried asking for information on the microsoft.public.platformsdk.security newsgroup?
    Tuesday, November 15, 2005 4:57 PM
    Moderator
  • The CreateTextFile method returns an open TextStream.  Since you never close this TextStream, it's still open when you try to call OpenAsTextStream.  This attempt to open a file that's already locked for writing is what's causing the exception you're seeing.

    You could fix the problem by calling the Close method on the initial TextStream.  However, since the initial call actually opens a TextStream with writing enabled, you could probably use the following as your "else" block:

    m_ts = m_fso.CreateTextFile(m_FileName)

    m_file = m_fso.GetFile(m_FileName)

    Wednesday, November 16, 2005 5:28 PM
  • THANKS ALOT, it seems to be working now. I;ve tired m_fso.CreateTextFile(m_FileName).Close() and did the same thing but it gave the same error for some reason, but this way seems to be working find..

    once again THANKS ALOT for all your help guys
    Thursday, November 17, 2005 7:12 PM