ASP.NET創(chuàng)建、刪除和移動(dòng)文件夾及文件夾列表功能
來(lái)源:易賢網(wǎng) 閱讀:1619 次 日期:2015-04-29 14:20:16
溫馨提示:易賢網(wǎng)小編為您整理了“ASP.NET創(chuàng)建、刪除和移動(dòng)文件夾及文件夾列表功能”,方便廣大網(wǎng)友查閱!

本文采用C#語(yǔ)言實(shí)現(xiàn)創(chuàng)建,刪除和移動(dòng)文件夾以及文件夾列表的功能,代碼如下:

使用Directory類(lèi)和DirectoryInfo類(lèi)

一、創(chuàng)建文件夾

try

...{

if (System.IO.Directory.Exists(DirectoryTextBox.Text))

...{

MsgLabel.Text = "該文件夾已經(jīng)存在";

return;

}

else

...{

System.IO.DirectoryInfo dirinfo = System.IO.Directory.CreateDirectory(DirectoryTextBox.Text);

MsgLabel.Text = "成功創(chuàng)建該文件夾!創(chuàng)建時(shí)間為:" + System.IO.Directory.GetCreationTime(DirectoryTextBox.Text);

}

}

catch (Exception ee)

...{

MsgLabel.Text = "處理失??! 失敗的原因是:" + ee.ToString();

}

二、刪除文件夾

try

...{

if (!Directory.Exists(DirectoryTextBox.Text))

...{

MsgLabel.Text = "該文件不存在";

}

else

...{

Directory.Delete(DirectoryTextBox.Text);

MsgLabel.Text = "刪除文件成功!";

}

}

catch (Exception ee)

...{

MsgLabel.Text = "操作失?。?失敗的原因是:" + ee.ToString();

}

三、移動(dòng)文件夾

源文件夾和目標(biāo)文件夾要求存在于同一個(gè)硬盤(pán)分區(qū)中否則會(huì)操作失敗(操作失?。?失敗原因:System.IO.IOException: 源路徑和目標(biāo)路徑必須具有相同的根。移動(dòng)操作在卷之間無(wú)效。 在

System.IO.Directory.Move(String sourceDirName, String destDirName) 在 CreateDirectory.MoveButton_Click(Object sender, EventArgs e) )

try

...{

if (!System.IO.Directory.Exists(SDirectoryTextBox.Text))

...{

Label1.Text = "源文件夾不存在!";

return;

}

if (System.IO.Directory.Exists(DDirectoryTextBox.Text))

...{

Label1.Text = "目標(biāo)文件夾已經(jīng)存在!";

return;

}

System.IO.Directory.Move(SDirectoryTextBox.Text, DDirectoryTextBox.Text);

Label1.Text = "文件夾移動(dòng)成功! 源文件已經(jīng)被移除。目標(biāo)文件夾為" + DFileTextBox.Text;

}

catch (Exception ee)

...{

Label1.Text = "操作失??! 失敗原因:" + ee.ToString();

}

四、文件夾列表

<table border="1">

<tr>

<td colspan="2" style="color: #660066;">

文件夾中文件列表:

</td>

</tr>

<tr>

<td style="font-size: 10pt">

請(qǐng)輸入要?jiǎng)h除文件的路徑:

</td>

<td style="width: 158px">

<asp:TextBox ID="DirectoryTextBox" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td colspan="2">

<asp:Label ID="MsgLabel" runat="server" ForeColor="red"></asp:Label>

</td>

</tr>

<tr>

<td colspan="2" style="width: 158px">

<asp:ListBox ID="FileListBox" runat="server" Height="192px" Width="184px"></asp:ListBox>

</td>

</tr>

<tr>

<td align="center" colspan="2">

<asp:Button ID="ExistButton" runat="server" Text="確定" OnClick="ExistButton_Click" />

</td>

</tr>

</table>

更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2026國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話(huà):0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)