Access is denied

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srinivasanr
    New Member
    • Jan 2009
    • 9

    Access is denied

    This is my code.

    When i execute this code, it throws "Access to the path "somepath" is denied."

    Code:
    FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(file);
                    try
                    {
                        sw.Write(vendorInfoStr.ToString());
                    }
    What is going wrong.

    Thanks,
    Srinivasan R.
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Originally posted by srinivasanr
    This is my code.

    When i execute this code, it throws "Access to the path "somepath" is denied."

    Code:
    FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(file);
                    try
                    {
                        sw.Write(vendorInfoStr.ToString());
                       
                    }
    What is going wrong.

    Thanks,
    Srinivasan R.
    What application are you using? if its asp.net then it doesnot have fileIO permission...or if you maynot have permission to create in that folder...

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      Code:
      using System.Security.Permissions;
      
      string filePath = @"C:\Zp\Er.txt";
       System.IO.FileInfo fname = new System.IO.FileInfo(filePath);
      
                    FileIOPermission permis = new FileIOPermission(FileIOPermissionAccess.AllAccess, fname.DirectoryName);
      
                    bool all = true;
                    try
                    {
                        permis.Demand();    
                        permis.PermitOnly();             
      
                    }
      
                    catch (System.Security.SecurityException ex)
                    {
                        all = false;
                    }
      
                    if (all)
                    {
      //do your stuff
      }

      Comment

      • srinivasanr
        New Member
        • Jan 2009
        • 9

        #4
        I am using only ASP.NET. But this code is working in my testing environment.
        But it is not working in my production server.

        This is the log file.

        Code:
        17:00:03 [9] ERROR VendorIntegration - Error while Transformming Vendor: 
        
        System.UnauthorizedAccessException: Access to the path 'some path' is denied.
        
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
        
           at 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)
        
           at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
        
           at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
        
           at DataAccess.Process.VendorIntegration.TransformSource(IList vendorInfoList)

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          You could either use impersonation How to implement impersonation in an ASP.NET application
          or give rights to asp.net on that particular...

          Comment

          • srinivasanr
            New Member
            • Jan 2009
            • 9

            #6
            thnaks for the reply.

            I have used the methods Impersonate the IIS authenticated account or user and it is working fine when i call the code through UI.

            But i have called that code automatically by using Thread concept.
            Now it throws the same error "UnAuthorizedAc cess" exception. Access is denied.

            What can i do for it?

            Comment

            • semomaniz
              Recognized Expert New Member
              • Oct 2007
              • 210

              #7
              You will have to give write permission to the folder that you are outputting the text file through the IIS.

              Comment

              • srinivasanr
                New Member
                • Jan 2009
                • 9

                #8
                Originally posted by semomaniz
                You will have to give write permission to the folder that you are outputting the text file through the IIS.
                I do not know how to give write permission to the destination folder through IIS. Please direct me.

                Comment

                • PRR
                  Recognized Expert Contributor
                  • Dec 2007
                  • 750

                  #9
                  Originally posted by srinivasanr
                  I do not know how to give write permission to the destination folder through IIS. Please direct me.
                  I am not sure whether this is the right way.. normally for testing i usually do this ... do ask a system admin for the "correct" way.
                  1. go to properties of the folder you want to give asp.net write rights.
                  2. Click security->add->advanced ->click on find now...Here you will find list of users... depending on what you have installed on your computer... click asp.net account and add and give permission to it ...

                  Comment

                  • srinivasanr
                    New Member
                    • Jan 2009
                    • 9

                    #10
                    I gave all permissions to all users and it is working when i call that code through UI. But it is not working when i call that same code through automated job by the use of Thread concept.

                    Comment

                    • PRR
                      Recognized Expert Contributor
                      • Dec 2007
                      • 750

                      #11
                      Originally posted by srinivasanr
                      I gave all permissions to all users and it is working when i call that code through UI. But it is not working when i call that same code through automated job by the use of Thread concept.
                      What do you mean by that?

                      Comment

                      • srinivasanr
                        New Member
                        • Jan 2009
                        • 9

                        #12
                        I mean i call the same code through Thread concept at a specified time.

                        Comment

                        • PRR
                          Recognized Expert Contributor
                          • Dec 2007
                          • 750

                          #13
                          Originally posted by srinivasanr
                          I mean i call the same code through Thread concept at a specified time.
                          Within the same application using a thread... shouldn't be a problem... Could you post some code and error message?

                          Comment

                          • srinivasanr
                            New Member
                            • Jan 2009
                            • 9

                            #14
                            Please see my first post...

                            Comment

                            • srinivasanr
                              New Member
                              • Jan 2009
                              • 9

                              #15
                              This is my code

                              Code:
                              FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write); 
                                              StreamWriter sw = new StreamWriter(file); 
                                              try 
                                              { 
                                                  sw.Write(vendorInfoStr.ToString()); 
                                
                                              }
                              This is my log message

                              Code:
                              17:00:03 [9] ERROR VendorIntegration - Error while Transformming Vendor:  
                                
                              System.UnauthorizedAccessException: Access to the path 'some path' is denied. 
                                
                                 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
                                
                                 at 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) 
                                
                                 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) 
                                
                                 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) 
                                
                                 at DataAccess.Process.VendorIntegration.TransformSource(IList vendorInfoList)

                              Comment

                              Working...