Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all

I am stuck at this point

I am crating a website to access some info and creating log file to trace if any error thrown during process.

this log file is created on a folder on network

i m using following code

C#
string dirss = @"\\network\dir_files\";
filePath.Value = dirss + @"Logs\log." + DateTime.Now.ToString("ddMMyyyy.HH") + ".log";



when i try to access this site it gives error

"Access to the path 'filePath.Value' is denied. "
Posted
Comments
Sujith Karivelil 24-Feb-15 6:32am    
same question exist:http://www.codeproject.com/Questions/879792/how-to-access-network-directory-using-userid-passw?arn=1
Tejas Vaishnav 25-Feb-15 0:50am    
Is my answer solve your problem, then please accept it as solution and don't forget to rate it.

1 solution

http://stackoverflow.com/questions/766033/copy-file-to-remote-computer-using-remote-admin-credentials[^]

please check the link, it will provide information about how to use user name and password to access the network location with the use of WindowsIdentity Impersonate



original post on stack overflow is in VB, i have converted it to c# as below..



C#
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
public class Form1
{
	[DllImport("advapi32.DLL", SetLastError = true)]
	public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
	private void Button1_Click(System.Object sender, System.EventArgs e)
	{
		IntPtr admin_token = default(IntPtr);
		WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
		WindowsIdentity wid_admin = null;
		WindowsImpersonationContext wic = null;
		try {
			MessageBox.Show("Copying file...");
			if (LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, ref admin_token) != 0) {
				wid_admin = new WindowsIdentity(admin_token);
				wic = wid_admin.Impersonate();
				System.IO.File.Copy("C:\\right.bmp", "\\\\157.60.113.28\\testnew\\right.bmp", true);
				MessageBox.Show("Copy succeeded");
			} else {
				MessageBox.Show("Copy Failed");
			}
		} catch (System.Exception se) {
			int ret = Marshal.GetLastWin32Error();
			MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
			MessageBox.Show(se.Message);
		} finally {
			if (wic != null) {
				wic.Undo();
			}
		}
	}
}


please modify code as per your requirement to create log file over network location
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900