-1

I am trying to create a TXT file using the system.io, i put in the file path but every time i run it my access is denied , to every folder i C: , in DISK D:everything is fine, is there a solution to it , by a solution i mean , can i get access to it somehow in the code?

the code i used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;

namespace KeyLoggerA1
{
    class Program
    {
        private static int WH_KEYBOARD_LL = 13; //hook procedure type , monitoring low-level keyboard input events
        private static int WM_KEYDOWN = 0x0100;
        private static IntPtr hook = IntPtr.Zero;
        private static LowLevelKeyboardProc llkProcedure = HookCallback;



        static void Main(string[] args)
        {
            string filePath = @"C:\Program Files\Test.txt";

            if (!File.Exists(filePath))
            {
                using (StreamWriter sw = File.CreateText(filePath))
                {
                    sw.WriteLine("test");
                }
            }

the error i am getting:

Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'C:\Program Files\Test.txt' is denied. ... at System.IO.File.CreateText(String path) at KeyLoggerA1.Program.Main(String[] args) in C:\Users\mirer\source\repos\ConsoleApp2\ConsoleApp2\Program.cs:line 28

If Anyone can help it will be appreciated

Ran Firrer
  • 43
  • 5
  • 4
    What is not clear about the error message? You don't have permissions to write into the "Program Files". Try your home directory for example. – jira Apr 27 '20 at 10:05
  • ProgramFiles and many other special folders on the system drive require elevated/administrator access. Try writing to the user's AppData instead to verify that this is the case. – pinkfloydx33 Apr 27 '20 at 11:37

3 Answers3

0

Make sure to use System.IO.Path.Combine() as it's the proper way for joining file paths.

Now for your question, that is totally normal. You just need to elevate the program (run as adminstrator) to able to access that folder. In case you want to check if program is currently being run as administrator or not, check this out, and this

vaiperboy
  • 88
  • 1
  • 8
  • 1
    What does `Path.Join` have to do with anything? It's only "proper" if you don't want the extra rooting logic built into `Path.Combine` and it's only available in dotnet core 2.1+ and netstandard 2.1. OP hasn't indicated what they are compiling for (Full framework, dotnet core, etc) so seems just like random unrelated advice. – pinkfloydx33 Apr 27 '20 at 11:42
  • I actually meant to say `Path.Combine`, my bad. – vaiperboy Apr 27 '20 at 11:51
0

The program doesn't have proper privileges to write to your destination. Try running the program as an administrator instead or saving to a different location (I know you're trying to save to program files so you might not be able to save to a different location)

0

Im have a think you make it your if have a folder in c for your text file you make it authentication settings bro