Hi, I have a question about delete specific file in a folder.I am using gridview to display the file like code below :
<div><b><u>LIST OF FILE</u></b><br /><br /></div>
<asp:SqlDataSource ID="SqlDataSourceDocument" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ></asp:SqlDataSource>
<asp:GridView ID="GridViewDocument" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSourceDocument" DataKeyNames="StudentDocID"
OnRowDeleting="GridViewDocument_RowDeleting" OnRowDataBound="GridViewDocument_RowDataBound"
CssClass="GridViewStyle" PageSize="5" Width="85%" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found"
EmptyDataRowStyle-HorizontalAlign="Center" EmptyDataRowStyle-Height="33">
<Columns>
<asp:TemplateField HeaderText="No"><ItemStyle width ="5%" VerticalAlign="Middle" HorizontalAlign="center" />
<ItemTemplate><%# Container.DataItemIndex + 1 %><asp:Label ID="lblStudentID" runat="server" Text='<%#Bind("StudentID") %>' Visible="false"></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File" Visible="True"><ItemStyle width ="30%" VerticalAlign="Middle" HorizontalAlign="left"/>
<ItemTemplate><asp:Label ID="lblFilename" runat="server" Text='<%#Bind("StudentDocName")%>'></asp:Label> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="10%">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" EnableViewState="false"
OnClientClick="return confirm('Are you sure you want to delete?');" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" />
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" HorizontalAlign="Center"/>
<EditRowStyle CssClass="EditRowStyle" />
</asp:GridView>
This is my back code to delete the file choosen :
protected void GridViewDocument_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
string StudentDocID = GridViewDocument.DataKeys[e.RowIndex].Value.ToString();
string StudentID = Request.QueryString["StudentID"];
SqlCommand cmdDocument = new SqlCommand("delete from StudentDocument where StudentDocID=" + StudentDocID, con);
con.Open();
cmdDocument.ExecuteNonQuery();
con.Close();
lblmsgStudent.Text = "Student document successfully deleted.";
}
Then already add the code below to delete the file in the folder:
foreach (string file in System.IO.Directory.GetFiles("C:\\Webapps\\sdms\\upload"))
{
if (System.IO.File.Exists(file))
{
System.IO.File.Delete(file);
}
}
My problem is, after I added the code, all the files in the folder is totally deleted. So how to get only the file (document ID) that has been choosen only be deleted?.
The second is, if there has same file name uploaded in the folder, how to add _copy or number at the back of the file name to differentiate the file name?
Kindly help me. Thank you.