Raysefo

Raysefo

  • 1.5k
  • 284
  • 150.1k

Writing TXT file to Share Problem?

Feb 25 2012 5:02 AM
Hi,

I am trying to write text file to a Share as follows:

IntPtr token = IntPtr.Zero;
                    bool isSuccess = LogonUser("UserName", "Domain", "Password!", LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token);

                    if (isSuccess)
                    {
                        using (WindowsImpersonationContext person = new WindowsIdentity(token).Impersonate())
                        {
                            using (StreamWriter error = new StreamWriter(@"\\IP\FileStore\logs\error_" + name + "_" + DateTime.Now.ToString("d") + ".txt", true))
                            {
                                error.Write(ex.Message);

                                error.Flush();

                                error.Close();
                            }

                            person.Undo();
                        }
                    }

And I am getting this error below, How can I fix this?

Best Regards.

System.IO.IOException was unhandled
  Message=Configuration information could not be read from the domain controller, either because the machine is unavailable, or access has been denied.

  Source=mscorlib
  StackTrace:
       konum: System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       konum: System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       konum: System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       konum: System.IO.StreamWriter.CreateFile(String path, Boolean append)
       konum: System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
       konum: System.IO.StreamWriter..ctor(String path, Boolean append)
       konum: ?.?(String A_0)
       konum: ?.?(IntPtr A_0, IntPtr A_1)
       konum: ?.?(Int32 A_0, IntPtr A_1, IntPtr A_2)
       konum: System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)
       konum: System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       konum: System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       konum: System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       konum: System.Windows.Forms.Application.Run(Form mainForm)
       konum: ?.?()
  InnerException: 


Answers (3)