-
[2008] Folder exists and create, file exists and delete, and process exists and end
Hello everyone! I am using Microsoft Visual Basic 2008 Express Edition. How do I check to see if a folder exists? If the folder exists, how do I check to see if a file in the folder exists? If the file exists, how do I check to see if the file is running? If the file is running, how do I stop running the file? How do I delete the file? If the folder does not exist, how do I create the folder? Thank you!
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Take a look at the System.IO.File and System.IO.Directory classes. They will do most of what you want.
There's really no way to check whether a file is "running". What does that really mean anyway? You can't "run" a data file. If it's an executable you can use the Process class to see if there's a corresponding process running but I doubt that it is because you wouldn't be deleting most executables.
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Quote:
Originally Posted by jmcilhinney
Take a look at the System.IO.File and System.IO.Directory classes. They will do most of what you want.
There's really no way to check whether a file is "running". What does that really mean anyway? You can't "run" a data file. If it's an executable you can use the Process class to see if there's a corresponding process running but I doubt that it is because you wouldn't be deleting most executables.
Okay, thank you. Yes, it is an executable. I took a look at the System.IO.File and System.IO.Directory classes and came up with the following code.
VB Code:
Module Module1
Sub Main()
If IO.Directory.Exists("C:\Folder") Then
If IO.File.Exists("C:\Folder\File.exe") Then
For Each Process As Process In Process.GetProcesses
If Process.ProcessName = "File.exe" Then
Process.Kill()
Exit For
End If
Next
IO.File.Delete("C:\Folder\File.exe")
End If
Else
IO.Directory.CreateDirectory("C:\Folder")
End If
End Sub
End Module
For some reason, the process is not killed and I am unable to delete the file as it is running. What is the problem? Thank you!
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Rather than getting all processes and testing the Name you should call GetProcessesByName.
If you try that code without the Delete call does it kill the process? If so then you're probably just trying to delete it too soon, before the system has had a chance to clean up properly.
You might try calling WaitForExit in between, although I'm not 100% sure that will work with Kill.
By the way, if this is a Windows Forms app then you should try calling CloseMainWindow before calling Kill. A forum search will turn up several examples.
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Quote:
Originally Posted by jmcilhinney
Rather than getting all processes and testing the Name you should call GetProcessesByName.
If you try that code without the Delete call does it kill the process? If so then you're probably just trying to delete it too soon, before the system has had a chance to clean up properly.
You might try calling WaitForExit in between, although I'm not 100% sure that will work with Kill.
By the way, if this is a Windows Forms app then you should try calling CloseMainWindow before calling Kill. A forum search will turn up several examples.
It does not kill the process when I try the code without the Delete call, although I have found the problem. The ProcessName property cannot include the file extension (exe). My code follows.
VB Code:
Module Module1
Sub Main()
If IO.Directory.Exists("C:\Folder") Then
If IO.File.Exists("C:\Folder\File.exe") Then
For Each Process As Process In Process.GetProcessesByName("File")
Process.Kill()
Exit For
Next
IO.File.Delete("C:\Folder\File.exe")
End If
Else
IO.Directory.CreateDirectory("C:\Folder")
End If
End Sub
End Module
Unfortunately, I have another problem. When it calls Process.Kill, I receive the following exception.
Quote:
System.ComponentModel.Win32Exception was unhandled
ErrorCode=-2147467259
Message="Access is denied"
NativeErrorCode=5
Source="System"
StackTrace:
at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited) at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited) at System.Diagnostics.Process.Kill() at ConsoleApplication1.Module1.Main() in C:\Documents and Settings\Jason\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Module1.vb:line 9 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
InnerException:
What is the problem? Thank you!
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Is that process running under a different user maybe?
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Quote:
Originally Posted by jmcilhinney
Is that process running under a different user maybe?
No, but when I try ending the process in Task Manager, it says the following.
Quote:
---------------------------
Unable to Terminate Process
---------------------------
This is a critical system process. Task Manager cannot end this process.
---------------------------
OK
---------------------------
How to I bypass this and kill the process anyways? Thank you!
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
You're kidding me, right? The Task Manager is telling you that this is a critical system process and you want to kill it and then delete the executable? Do you know what "critical" means?
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Perhaps that reaction was a little string but this does seem to be a rather wacky request. If this process is critical then it's highly likely that your OS will fail or misbehave if you do manage to kill the process, let a lone delete the executable. Perhaps you should explain what exactly you're trying to achieve and why. Deleting critical files is also something that malicious software might do, so what are we to think?
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Quote:
Originally Posted by jmcilhinney
You're kidding me, right? The Task Manager is telling you that this is a critical system process and you want to kill it and then delete the executable? Do you know what "critical" means?
haha strangely I thought the exact same thing when I read his post...
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Quote:
Originally Posted by jmcilhinney
Perhaps that reaction was a little string but this does seem to be a rather wacky request. If this process is critical then it's highly likely that your OS will fail or misbehave if you do manage to kill the process, let a lone delete the executable. Perhaps you should explain what exactly you're trying to achieve and why. Deleting critical files is also something that malicious software might do, so what are we to think?
Sorry for the misunderstanding. I am creating a program that will remove a virus by killing the process than deleting the file. The process is not actually critical. It just says that to prevent you from killing it. How do I bypass this and kill the process anyways? Thank you!
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
sounds like he's making a kill switch program. I made one a while back that deleted c:\program files but i have no idea where exactly that source is but it sounds like it would help
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
How would deleting the entire Program Files directory be useful for anything other than making a virus??
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Quote:
Originally Posted by Shadowz O Death
Sorry for the misunderstanding. I am creating a program that will remove a virus by killing the process than deleting the file. The process is not actually critical. It just says that to prevent you from killing it. How do I bypass this and kill the process anyways? Thank you!
How do I do this? Someone must know how... I tried to stop the virus from opening at startup by making my program edit the registry, but the virus makes itself open at startup again by editing the registry when I shutdown my computer.
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
Perhaps you should research that specific virus online and see what's required to remove it. This is not a VB.NET issue as you're not going to get rid of something like this using an amateurish VB.NET program.
-
Re: [2008] Folder exists and create, file exists and delete, and process exists and end
you don't need to make a program you need a better AV/AS program. Use zone alarm since its sensitive to viruses.
If it sets itself to start up it should register itself on the start up utilities aswell so check
run>msconfig>start up programs
for the file and disable it then
Ctrl + Alt + Del> Processes> View all processes
close the process