none
"The action can't be completed because the folder or a file in it is open in another program" RRS feed

  • Question

  • I have build a File Explorer in my C# application and when I am deleting folder/file in Windows Explorer, it does and I don't have this message.

    I have such message when I am using Unreal Engine and I would have the same.

    I don't want to be able to delete the folder/files in Windows Explorer, that I am using in my C# app.

    How can I do that ?

    I have read that https://stackoverflow.com/questions/25398264/lock-folder-using-c-sharp-and-want-access-files-from-this-folder

    But the problem is that it create a temporary user to lock the file and it does generate not the same message, when trying to delete it from Windows Explorer and it's not what I want.

    Friday, December 13, 2019 12:58 AM

Answers

  • Hi Dilly0,

    Thank you for posting here.

    If you want to lock a file, you can use the following two methods.

    1. When using File Stream, set File Share to None.

    FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.None);

    2. FileStream.lock ()

    If you want to lock folders, I'm afraid DirectorySecurity is the most effective way.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dilly0 Friday, December 13, 2019 7:25 PM
    Friday, December 13, 2019 5:34 AM

All replies

  • Hi Dilly0,

    Thank you for posting here.

    If you want to lock a file, you can use the following two methods.

    1. When using File Stream, set File Share to None.

    FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.None);

    2. FileStream.lock ()

    If you want to lock folders, I'm afraid DirectorySecurity is the most effective way.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dilly0 Friday, December 13, 2019 7:25 PM
    Friday, December 13, 2019 5:34 AM
  • Why does this method is not considered as a virus ?

    I tried the method DirectorySecurity and I locked a folder by mistake and passed it to a recycle bin with Unlocker program.

    I locked the folder, but not unlocked it after closing the program.

    After three days of research how to delete this folder, I found this way

                var Shl = new Shell();
                Folder Recycler = Shl.NameSpace(10);
                FolderItems items = Recycler.Items();
                for (int i = 0; i < items.Count; i++)
                {
                    try
                    {
                        FolderItem FI = items.Item(i);
                        string FileName = Recycler.GetDetailsOf(FI, 0);
                        string FilePath = Recycler.GetDetailsOf(FI, 1);
                        string RecyleDate = Recycler.GetDetailsOf(FI, 2);
                        if (FileName == "Maps")
                        {
                            // check if chosen item is a folder
                            if (FI.IsFolder)
                            {
                                try
                                {
                                    string folderPath = FI.Path;
                                    string adminUserName = Environment.UserName;// getting your adminUserName
                                    DirectorySecurity ds = Directory.GetAccessControl(folderPath);
                                    FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny);
                                    ds.RemoveAccessRule(fsa);
                                    Directory.SetAccessControl(folderPath, ds);
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                                Directory.Delete(FI.Path, true);
                            }
                            else
                            {
                                File.Delete(FI.Path);
                            }
                        }
                    }
                    catch { }
                }

    Trying to delete the folder through the GUI is impossible, only possible programmatically.

    And I am sure that virus use this method to lock the infected programs.

    Windows deny the access to HID packets when RID is at zero when the sniffer operate on user mode access.

    It's to prevent spying software to steal passwords, so why didn't they do the same for this DirectorySecurity method used by Virus/Trojan/Malware/ect to do unwanted operations ?


    • Edited by Dilly0 Wednesday, December 18, 2019 8:33 PM
    Wednesday, December 18, 2019 8:26 PM