开发者

VC++ CLI/CLR delete files/folder over the network

I wish开发者_开发问答 to delete files from a network Pc. The user has full control over a shared folder on the PC from which to delete files. I have this code :

if(status)
        {
            if(File::Exists(selectedfile))
                System::IO::File::Delete(selectedfile);
            else
                MessageBox::Show("File does not exist.");

        }
        else
        {   
            if(!System::IO::Directory::Exists(selectedfile))
                MessageBox::Show("The directory does not exists.");
            try{
                System::IO::Directory::Delete(selectedfile,true);
                if(System::IO::Directory::Exists(selectedfile))
                {
                    deleted =false;
                    System::IO::Directory::Delete(selectedfile,true);
                }
                else
                    deleted = true;
            }

I included the second delete in the Directory loop because the folder is not deleted at first attempt, only the files inside the folder are deleted. However, I get an Access denied whenever I try to delete the empty folder.

How to make sure that the directory and all it's content are deleted.


This is quite normal, one of the things that a multi-tasking operating system needs to do. The directory is in fact marked for deletion but it cannot be removed yet because one or more processes has a handle open on the directory. In the case of Windows, that is commonly a process that uses the the directory as its default working directory. Or maybe you've got an Explorer window open, looking at how your program is doing its job. Explorer uses ReadDirectoryChangesW() to get notified about changes in the directory so it knows when to refresh the view.

The directory will be physically removed from the drive as soon as the last handle is closed. While it exists in this zombified state, any attempt to do anything with the directory will produce an access error (Windows error code 5).

You'll need to account for this behavior in your program. Definitely remove the second Directory::Exists() test, when you didn't get an exception from the Delete call you'll need to assume that the directory got deleted. That will be accurate, eventually.


You need file server functionality on computer A and B and write a client on computer C.

  • The server could be a kind of FTP server, where you have to explicity configure which directories are handled on both sites.

  • The server can be a Windows share. You can use UNC file names to address these files and use the Windows API on computer C. When you have mapped network drives at computer C you can work with the network files as you would do with local files.
    The computers A and B must be configured so that there are shares with sufficient rights.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜