Results 1 to 6 of 6

Thread: System.UnauthorizedAccessException

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    System.UnauthorizedAccessException

    Hello There,

    i made a simple program to edit the notepad file...using C# ..but i get this error

    Code:
    System.UnauthorizedAccessException: Access to the path "C:\MyFolder\MySystem\MySystem.INI" is denied.
    when i try to run it in a user that has no administrative privileged...

    is there any way that it forcely edit the file?..i dnt know what method will be use...right now,i study impersonation but i don't know if this is the solution...

    this my code...
    Code:
    private void Form1_Load(object sender, System.EventArgs e)
    		{
    			try
    			{
    				ReplaceInFile(@"C:\MyFolder\MySystem\MySystem.INI","58.71.17.62","mlserver.company.com");
    				ReplaceInFile(@"C:\MyFolder\MySystem\MySystem.INI","192.168.12.100","mlserver.company.com");
    				ReplaceInFile(@"C:\MyFolder\MySystem\MySystem.INI","mlmysqlchatra","mlserver.company.com");
    			}
    			catch (Exception ex)
    			{
    				throw ex;
    			}
    		}
    Code:
    static public void ReplaceInFile(string filePath,string searchText,string replaceText)
    		{
    						
    			StreamReader reader = new StreamReader(filePath);
    			string content = reader.ReadToEnd();
    			reader.Close();
    
    			content = Regex.Replace(content,searchText,replaceText);
    			
    			StreamWriter writer = new StreamWriter(filePath);
    			writer.Write(content);
    			writer.Close();
    
    		}
    hope you can help me..thank you

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: System.UnauthorizedAccessException

    Which operating system are you using?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: System.UnauthorizedAccessException

    im using windows xp.

  4. #4
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: System.UnauthorizedAccessException

    So you are saying that you want to edit a file that requires admin privs to edit without having these privs?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: System.UnauthorizedAccessException

    Quote Originally Posted by Cyb3rH4Xter View Post
    So you are saying that you want to edit a file that requires admin privs to edit without having these privs?
    yes sir..right now i was thinking of using impersonation...but i dont know if this is the solution...

  6. #6
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: System.UnauthorizedAccessException

    I am sorry but I don't know a way to do that.
    Found this thread:
    http://stackoverflow.com/questions/1...istrative-user

    Where the conclusion is that you can't edit it without the admin rights. It would kinda be a flaw in Windows if you could do that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width