Function is not running and creating the text file


Function is not running and creating the text file



I created the function pro:



the write function writeproc:



when i run the code it is not doing anything.... :(
This is the main method for the console app that i have made in c#.


[STAThread]
static void Main(String args)
{
pro();
}



pro function:


static void pro()
{

Process localAll = Process.GetProcesses();
String path_pro = "C://KEYLOGS//processes.txt";
foreach(Process proc in localAll)
{
writeproc(path_pro, proc);
}
}



writeproc function:


static void writeproc(String p, Process the_process)
{
if (!File.Exists(p))
{
using (StreamWriter sw = File.CreateText(p))
{
//empty file generated.
}
}
else
{
using (StreamWriter sw = File.AppendText(p))
{
sw.WriteLine("Process: "+the_process);
}
}
}





Do you run it as administrator? Files so close to the root C: are likely to be protected
– Rafalon
Jul 3 at 6:36


C:





well other functions are able to write files to the c: directory but only this function is not doing anything
– Lakshay Bhalla
Jul 3 at 6:37





Which other functions? Can you find any difference between those which work and this one?
– Rafalon
Jul 3 at 6:37





yes those functions contain both arguement as String but this one contains one String path and the other as process.
– Lakshay Bhalla
Jul 3 at 6:38





Did you check if localAll was empty or not?
– Rafalon
Jul 3 at 6:40


localAll




1 Answer
1



This may be the cause of two different things.



1: The folder does not exist on your C drive so the file can't be created. (It will throw a System.IO.DirectoryNotFoundException)
Add Directory.CreateDirectory(p); to the start of your writeproc method.


System.IO.DirectoryNotFoundException


Directory.CreateDirectory(p);



2: You don't have enough rights to write to your C drive. (It will throw a System.UnauthorizedAccessException)


System.UnauthorizedAccessException



I suggest adding a breakpoint in your writeproc method to see what exception is being thrown.





or localAll in his pro function is empty
– Rafalon
Jul 3 at 6:41


localAll


pro





@Rafalon, That's right, it is theoretically possible. However I don't know about a situation when Process.GetProcesses() results in an empty array.
– Bart
Jul 3 at 6:44



Process.GetProcesses()





it did work thanks. but still i don't get it . i already had the directory created then why do i require to create it again?
– Lakshay Bhalla
Jul 3 at 6:44





Well, the method creates the directory if it doesn't exist. If it exists it will just return the DirectoryInfo of the existing directory. So it looks like the directory didn't exist after all.
– Bart
Jul 3 at 6:47







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails