Skip to main content
 首页 » 编程设计

c#之如何打开目录以在其上流式传输

2024年02月13日16Renyi-Fan

我已经构建了一个应用程序和一个可以在彼此之间流式传输文件的服务,但我的代码似乎只能在闪存驱动器上运行,它应该读取闪存驱动器和 CD 上的目录。

    foreach (RadTreeNode l_node in m_fileExplorer.SelectedNodes) { 
 
        string l_sSelectedPath = l_node.Name; 
        FileAttributes l_fileAttr = File.GetAttributes(l_sSelectedPath); 
 
        if (l_fileAttr == FileAttributes.Directory) { 
            foreach (string l_sFilename in Directory.GetFiles(l_sSelectedPath, "*.*", SearchOption.AllDirectories)) { 
                m_qUpload.Enqueue( 
                    new UploadDescriptor { 
                        BatchDescription = m_tbDescription.Text, 
                        BatchTimestamp = l_now, 
                        BatchId = l_sBatchId, 
                        Username = m_frmLogin.Username, 
                        TargetUsername = l_sTargetUsername, 
                        Filename = l_sFilename 
                    } 
                ); 
 
                AddProgressRow(l_sFilename); 
                l_nFilesInBatch++; 
                ms_sem.Release(); 
            } 
        } 
    } 

当我尝试对 CD 执行相同操作时,出现此错误:

System.UnauthorizedAccessException: Access to the path 'D:\' is denied. 
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) 
   at System.IO.File.Open(String path, FileMode mode, FileAccess access) 
   at ActivePath.FileUploader.Frontend.UploadForm.UploadEx(Object sender, DoWorkEventArgs e) in c:\code\Customers\FIBI\PWFileUploader\PWFileUploaderApplication\UploadForm.cs:line 697 

看来我不能使用相同的代码来完成这两项操作,但我不知道该怎么做。

请您参考如下方法:

从堆栈跟踪的外观来看,某些东西正在调用 File.Open。我假设 UploadEx 是您编写的并且您可以控制的东西。

检查以确保 FileMode和 FileAccess值是正确的。


这应该没问题,因为 FileMode.Open 说文件只有存在时才应该打开,而 FileAccess.Read 将阻止写入文件:

File.Open(somePath, FileMode.Open, FileAccess.Read); 

这些可能不会,因为如果文件不存在,您的程序将尝试创建它,这在 cd-rom 上运行不佳。或者,如果您的代码无意中修改了文件,那也不会起作用。

File.Open(somePath, FileMode.OpenOrCreate); 
 
File.Open(somePath, FileMode.Open, FileAccess.ReadWrite); 

从你的代码来看,你似乎除了读取目录中的所有文件之外没有做任何事情,但我看不到你的所有代码所以我只是尽可能地把它扔在那里回答。


好的,根据我在评论中链接的那篇文章,这里有一个适合您的解决方案。我实际上并没有构建它,所以可能会有一两个语法错误。如果不起作用,请给我留言。

你是当前的 foreach 循环:

    foreach (RadTreeNode l_node in m_fileExplorer.SelectedNodes) 
    { 
        string l_sSelectedPath = l_node.Name; 
 
        FileAttributes l_fileAttr = File.GetAttributes(l_sSelectedPath); 
 
        if (l_fileAttr == FileAttributes.Directory) 
            DoWhatever(l_sSelectedPath); 
    } 

一种递归扫描每个目录的新方法,忽略抛出异常的目录:

private void DoWhatever(string path) 
{ 
    try 
    { 
        Directory.GetFiles(path) 
                 .ToList() 
                 .ForEach(l_sFilename => 
                 { 
                     m_qUpload.Enqueue( 
                         new UploadDescriptor { 
                             BatchDescription = m_tbDescription.Text, 
                             BatchTimestamp = l_now, 
                             BatchId = l_sBatchId, 
                             Username = m_frmLogin.Username, 
                             TargetUsername = l_sTargetUsername, 
                             Filename = l_sFilename 
                         } 
                     ); 
 
                     AddProgressRow(l_sFilename); 
                     l_nFilesInBatch++; 
                     ms_sem.Release(); 
                 }); 
 
        Directory.GetDirectories(path) 
                 .ToList() 
                 .ForEach(s => DoWhatever(s)); 
    } 
    catch (UnauthorizedAccessException ex) 
    { 
        // We're not authorized to access this directory. Who knows why. Ignore it. 
    } 
}