C#批量删除指定文件夹下指定文件名的所有文件夹

<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">private</span> <span style="color: blue;">void</span> DeleteDirByName(<span style="color: blue;">string</span> rootPath, <span style="color: blue;">string</span> name)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">string</span> dirName = rootPath;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span>(rootPath.EndsWith(<span style="color: #a31515;">"\\"</span>)||rootPath.EndsWith(<span style="color: #a31515;">"/"</span>))</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>rootPath = rootPath.Substring(0,rootPath.Length-1);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> indexSplit = rootPath.LastIndexOf(<span style="color: #a31515;">'\\'</span>);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span>(indexSplit&lt;0)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>indexSplit = rootPath.LastIndexOf(<span style="color: #a31515;">'/'</span>);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style=""></span><span style="color: blue;">if</span>(indexSplit&gt;0)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>dirName = rootPath.Substring(indexSplit + 1);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span> (dirName.ToLower() == name.ToLower())</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">this</span>.SetFileAttributes(rootPath);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: #2b91af;">Directory</span>.Delete(rootPath, <span style="color: blue;">true</span>);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">this</span>.textBox3.Text += rootPath + <span style="color: #2b91af;">Environment</span>.NewLine;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">string</span>[] subDirs = <span style="color: #2b91af;">Directory</span>.GetDirectories(rootPath);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> subDir <span style="color: blue;">in</span> subDirs)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">this</span>.DeleteDirByName(subDir, name);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">private</span> <span style="color: blue;">void</span> SetFileAttributes(<span style="color: blue;">string</span> path)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">string</span>[] files = <span style="color: #2b91af;">Directory</span>.GetFiles(path);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> file <span style="color: blue;">in</span> files)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: #2b91af;">File</span>.SetAttributes(file, <span style="color: #2b91af;">FileAttributes</span>.Normal);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">string</span>[] subDirs = <span style="color: #2b91af;">Directory</span>.GetDirectories(path);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> subDir <span style="color: blue;">in</span> subDirs)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span><span style="color: blue;">this</span>.SetFileAttributes(subDir);</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt;" align="left"><span style="font-family: NSimSun; font-size: 9.5pt;" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"></span></span></p>

你可能感兴趣的:(文件夹)