Question : Giving permission for stream writer to write in C:/ drive

Hi, I am trying to write to a local C:/ drive using stream writer. it says that i do not have permission to write in c:/ drive. Is there anyway to set those permission in my code.
1:
2:
3:
4:
5:
6:
7:
8:
9:
private static void WriteLog(string filePath, string message)
        {
            using( var x = new StreamWriter( filePath, true ) )
            {
                x.Write( DateTime.Now );
                x.Write( "\t" );
                x.WriteLine( message );
            }
        }

Answer : Giving permission for stream writer to write in C:/ drive

Great.

ok here we go.

Step 1.

Create a local account on your pc username: filewriter and password: FileWriter1 (or something you fancy).

Step 2.

Give that user permissions to Read/Write/Execute on C Drive or a folder to which you will write your log to. (Which I believe should be the way. A folder to which that user account will have access and not the root C drive).

Step 3.

Create a new class file and name is Security.cs

Add the code to that class


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;

public class Security
{

    private IntPtr tokenHandle = new IntPtr(0);

    private WindowsImpersonationContext impersonatedUser;

    [DllImport("kernel32.dll")]
    static extern bool CloseHandle(IntPtr handle);

    //  Test harness.
    //  If you incorporate this code into a DLL, be sure to demand FullTrust.
    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    public bool ImpersonateStart(string sDomainName, string userName, string Password)
    {
        bool returnValue;
        WindowsIdentity newId = null;
        try
        {
            tokenHandle = IntPtr.Zero;
            //  Call LogonUser to obtain a handle to an access token.
            returnValue = false;
            const int LOGON32_PROVIDER_DEFAULT = 0;
            //This parameter causes LogonUser to create a primary token.
            const int LOGON32_LOGON_INTERACTIVE = 2;

            returnValue = LogonUser(userName, sDomainName, Password , LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

            // check if logon successful
            if ((returnValue == false))
            {
                int ret = Marshal.GetLastWin32Error();
                throw new System.ComponentModel.Win32Exception(ret);
            }
            else
            {
                // Logon succeeded
                //  Use the token handle returned by LogonUser.
                newId = new WindowsIdentity(tokenHandle);
                impersonatedUser = newId.Impersonate();
            }
        }
        catch (Exception ex)
        {
            ex = null;
            returnValue = false;
        }
        finally
        {
            if (!(newId == null))
            {
                newId.Dispose();
                newId = null;
            }
        }
        return returnValue;
    }

    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    public void ImpersonateStop()
    {
        try
        {
            //  Stop impersonating the user.
            impersonatedUser.Undo();
            //  Free the tokens.
            if (tokenHandle != IntPtr.Zero)
            {
                CloseHandle(tokenHandle);
            }
        }
        catch (System.Exception Return)
        {
        }
    }

    [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
        int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

}
Random Solutions  
 
programming4us programming4us