By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Access is denied

P: 9
This is my code.

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

Expand|Select|Wrap|Line Numbers
  1. FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  2.                 StreamWriter sw = new StreamWriter(file);
  3.                 try
  4.                 {
  5.                     sw.Write(vendorInfoStr.ToString());
  6.                 }
What is going wrong.

Thanks,
Srinivasan R.
Jan 22 '09 #1
Share this Question
Share on Google+
14 Replies


Expert 100+
P: 750
PRR
@srinivasanr
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...
Jan 22 '09 #2

Expert 100+
P: 750
PRR
Expand|Select|Wrap|Line Numbers
  1. using System.Security.Permissions;
  2.  
  3. string filePath = @"C:\Zp\Er.txt";
  4.  System.IO.FileInfo fname = new System.IO.FileInfo(filePath);
  5.  
  6.               FileIOPermission permis = new FileIOPermission(FileIOPermissionAccess.AllAccess, fname.DirectoryName);
  7.  
  8.               bool all = true;
  9.               try
  10.               {
  11.                   permis.Demand();    
  12.                   permis.PermitOnly();             
  13.  
  14.               }
  15.  
  16.               catch (System.Security.SecurityException ex)
  17.               {
  18.                   all = false;
  19.               }
  20.  
  21.               if (all)
  22.               {
  23. //do your stuff
  24. }
  25.  
Jan 22 '09 #3

P: 9
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.

Expand|Select|Wrap|Line Numbers
  1. 17:00:03 [9] ERROR VendorIntegration - Error while Transformming Vendor: 
  2.  
  3. System.UnauthorizedAccessException: Access to the path 'some path' is denied.
  4.  
  5.    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  6.  
  7.    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)
  8.  
  9.    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
  10.  
  11.    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
  12.  
  13.    at DataAccess.Process.VendorIntegration.TransformSource(IList vendorInfoList) 
Jan 22 '09 #4

Expert 100+
P: 750
PRR
You could either use impersonation How to implement impersonation in an ASP.NET application
or give rights to asp.net on that particular...
Jan 22 '09 #5

P: 9
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 "UnAuthorizedAccess" exception. Access is denied.

What can i do for it?
Jan 23 '09 #6

Expert 100+
P: 210
You will have to give write permission to the folder that you are outputting the text file through the IIS.
Jan 27 '09 #7

P: 9
@semomaniz
I do not know how to give write permission to the destination folder through IIS. Please direct me.
Jan 28 '09 #8

Expert 100+
P: 750
PRR
@srinivasanr
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 ...
Jan 28 '09 #9

P: 9
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.
Jan 28 '09 #10

Expert 100+
P: 750
PRR
@srinivasanr
What do you mean by that?
Jan 28 '09 #11

P: 9
I mean i call the same code through Thread concept at a specified time.
Jan 28 '09 #12

Expert 100+
P: 750
PRR
@srinivasanr
Within the same application using a thread... shouldn't be a problem... Could you post some code and error message?
Jan 28 '09 #13

P: 9
Please see my first post...
Jan 28 '09 #14

P: 9
This is my code

Expand|Select|Wrap|Line Numbers
  1. FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write); 
  2.                 StreamWriter sw = new StreamWriter(file); 
  3.                 try 
  4.                 { 
  5.                     sw.Write(vendorInfoStr.ToString()); 
  6.  
  7.                 } 
This is my log message

Expand|Select|Wrap|Line Numbers
  1. 17:00:03 [9] ERROR VendorIntegration - Error while Transformming Vendor:  
  2.  
  3. System.UnauthorizedAccessException: Access to the path 'some path' is denied. 
  4.  
  5.    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
  6.  
  7.    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) 
  8.  
  9.    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) 
  10.  
  11.    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) 
  12.  
  13.    at DataAccess.Process.VendorIntegration.TransformSource(IList vendorInfoList)  
Jan 28 '09 #15

Post your reply

Sign in to post your reply or Sign up for a free account.