Results 1 to 16 of 16

Thread: [2008] Folder exists and create, file exists and delete, and process exists and end

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Location
    White Oak, PA
    Posts
    25

    [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!
    Last edited by Shadowz O Death; Aug 8th, 2008 at 12:14 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Location
    White Oak, PA
    Posts
    25

    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:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         If IO.Directory.Exists("C:\Folder") Then
    5.             If IO.File.Exists("C:\Folder\File.exe") Then
    6.                 For Each Process As Process In Process.GetProcesses
    7.                     If Process.ProcessName = "File.exe" Then
    8.                         Process.Kill()
    9.                         Exit For
    10.                     End If
    11.                 Next
    12.                 IO.File.Delete("C:\Folder\File.exe")
    13.             End If
    14.         Else
    15.             IO.Directory.CreateDirectory("C:\Folder")
    16.         End If
    17.     End Sub
    18.  
    19. 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!
    Last edited by Shadowz O Death; Aug 8th, 2008 at 01:04 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Location
    White Oak, PA
    Posts
    25

    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:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         If IO.Directory.Exists("C:\Folder") Then
    5.             If IO.File.Exists("C:\Folder\File.exe") Then
    6.                 For Each Process As Process In Process.GetProcessesByName("File")
    7.                     Process.Kill()
    8.                     Exit For
    9.                 Next
    10.                 IO.File.Delete("C:\Folder\File.exe")
    11.             End If
    12.         Else
    13.             IO.Directory.CreateDirectory("C:\Folder")
    14.         End If
    15.     End Sub
    16.  
    17. End Module
    Unfortunately, I have another problem. When it calls Process.Kill, I receive the following exception.
    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!
    Last edited by Shadowz O Death; Aug 8th, 2008 at 02:09 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Folder exists and create, file exists and delete, and process exists and end

    Is that process running under a different user maybe?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Location
    White Oak, PA
    Posts
    25

    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.
    ---------------------------
    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!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Location
    White Oak, PA
    Posts
    25

    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!

  12. #12
    New Member r33k's Avatar
    Join Date
    Jul 2008
    Location
    New Jersey
    Posts
    9

    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

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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??
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Location
    White Oak, PA
    Posts
    25

    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.
    Last edited by Shadowz O Death; Aug 9th, 2008 at 07:52 PM.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    New Member r33k's Avatar
    Join Date
    Jul 2008
    Location
    New Jersey
    Posts
    9

    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

    Quote Originally Posted by HBDev
    Being that this is illegal in 71 U.S. states including New Failico, Califailia, and Failida you are sentenced to death by lethal injection of fail.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width