1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to search a file in computer and delete it in VB.Net

Discussion in 'C#' started by alamlinks, Jul 2, 2013.

  1. #1
    Hello Friends,

    I want to search a file in my computer and want to delete the file in VB.Net

    But I am getting delete a file when it's path is know, But I don't want to give the path. I want to give the path by searching it.

    If any one know it's solution then please Do reply.

    Thanks & Regards
     
    alamlinks, Jul 2, 2013 IP
  2. ByteCoder

    ByteCoder Active Member

    Messages:
    239
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    65
    #2
    So I googled for a second and found:
    For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
        My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
        FileIO.SearchOption.SearchAllSubDirectories, "*")
     
        My.Computer.FileSystem.DeleteFile(foundFile)
     
    Next
    Code (markup):
    This code will search your MyDocuments folder and all its sub-folders for all files and delete them.
    "*" is the pattern for ALL, you can use "*.txt" to file text files only or just "haha*" to delete any file that its name starts with "haha", no matter how it continues.
     
    ByteCoder, Jul 5, 2013 IP
  3. alamlinks

    alamlinks Well-Known Member

    Messages:
    992
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks, But Error:
    Access to the path 'C:\Users\NOOR\Documents\My Music' is denied.
     
    alamlinks, Jul 5, 2013 IP
  4. Chris Santos

    Chris Santos Member

    Messages:
    25
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    38
    Digital Goods:
    1
    #4
    You could always try and mess with the folder permissions.

    Dim FolderPath As String = "C:\test\myfolder"
     
    Dim UserAccount As String = "domain1\testuser"
     
    Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPath)
     
    Dim FolderAcl As New DirectorySecurity
     
    FolderAcl = FolderInfo.GetAccessControl()
     
    FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
     
    FolderInfo.SetAccessControl(FolderAcl)
    Code (markup):
    Or change your test user for the app.

    File permissions can be a headache.
     
    Chris Santos, Jul 6, 2013 IP
  5. kks21199

    kks21199 Active Member

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #5
    Just go to Project Settings -> Application Tab and click on the "View UAC Settings" or "View windows settngs" button.

    In the new tab opened, you will see a code like this,


    <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <!-- UAC Manifest Options
                If you want to change the Windows User Account Control level replace the
                requestedExecutionLevel node with one of the following.
    
            <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
            <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
    
                Specifying requestedExecutionLevel node will disable file and registry virtualization.
                If you want to utilize File and Registry Virtualization for backward
                compatibility then delete the requestedExecutionLevel node.
            -->
            <requestedExecutionLevel level="highestAvailable" uiAccess="false" />     'the line you need to paste it to
          </requestedPrivileges>
    Code (markup):
    from the comment copy any permssion you want and paste it before the ending requestedprivileges tag and delete the old lne there.

    This wll give you permissions to have admin rights and to change or delete folders from anywhere in the computer.
     
    kks21199, Nov 7, 2013 IP