VC++ CLI/CLR 通过网络删除文件/文件夹
我想从网络电脑上删除文件。用户可以完全控制 PC 上的共享文件夹并从中删除文件。 我有这样的代码:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是很正常的,是多任务操作系统需要做的事情之一。该目录实际上已标记为删除,但尚无法删除,因为一个或多个进程在该目录上打开了一个句柄。对于 Windows,这通常是一个使用该目录作为其默认工作目录的进程。或者您可能打开了一个资源管理器窗口,查看您的程序如何完成其工作。 Explorer 使用 ReadDirectoryChangesW() 获取有关目录更改的通知,以便知道何时刷新视图。
一旦最后一个句柄关闭,该目录就会从驱动器中物理删除。当它以僵尸状态存在时,任何对该目录执行任何操作的尝试都会产生访问错误(Windows 错误代码 5)。
您需要在程序中考虑这种行为。绝对删除第二个 Directory::Exists() 测试,当您没有从删除调用中得到异常时,您需要假设该目录已被删除。最终这将是准确的。
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.
您需要计算机 A 和 B 上的文件服务器功能,并在计算机 C 上编写客户端。
服务器可以是一种 FTP 服务器,您必须在其中显式配置在两个站点上处理哪些目录。
服务器可以是 Windows 共享。您可以使用 UNC 文件名来寻址这些文件,并使用计算机 C 上的 Windows API。在计算机 C 上映射网络驱动器后,您可以像处理本地文件一样处理网络文件。
计算机 A 和 B 必须配置为具有足够权限的共享。
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.