Hi Prasunjeet,
Check this example. Now please take its reference and correct your code.
HTML
<div>
<a href="Files/Test.doc">Test.doc</a><hr />
<img src="Files/Test.jpg" height="100px" width="100px" />
<br />
<input type="button" id="btnOnCheck" value="Check File" />
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btnOnCheck').on('click', function () {
var anchors = $('a');
var images = $('img');
var filesUsed = [];
$.each(anchors, function () {
filesUsed.push($(this).attr('href'));
});
$.each(images, function () {
filesUsed.push($(this).attr('src'));
});
$.ajax({
type: "POST",
url: "Default.aspx/CheckFiles",
data: JSON.stringify({ files: filesUsed }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var message = "Unused Files are\n----------------------\n";
$.each(r.d, function () {
message += $(this)[0] + "\n";
});
alert(message);
}, error: function (r) {
alert(r.d);
}
});
});
});
</script>
Code
C#
[System.Web.Services.WebMethod]
public static string[] CheckFiles(string[] files)
{
List<string> filesUsed = new List<string>();
foreach (string file in files)
{
filesUsed.Add(HttpContext.Current.Server.MapPath("~/") + file.Replace("/", "\\"));
}
string[] filePaths = System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("~/Files/"));
List<string> filesUnUsed = (from s in filePaths
where !filesUsed.Contains(s)
select System.IO.Path.GetFileName(s)).ToList();
return filesUnUsed.ToArray(); ;
}
VB.Net
<System.Web.Services.WebMethod()>
Public Shared Function CheckFiles(ByVal files As String()) As String()
Dim filesUsed As List(Of String) = New List(Of String)()
For Each file As String In files
filesUsed.Add(HttpContext.Current.Server.MapPath("~/") & file.Replace("/", "\"))
Next
Dim filePaths As String() = System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("~/Files/"))
Dim filesUnUsed As List(Of String) = (From s In filePaths _
Where Not filesUsed.Contains(s) _
Select System.IO.Path.GetFileName(s)).ToList()
Return filesUnUsed.ToArray()
End Function
Screenshot
![](https://i.imgur.com/9PcVZqz.jpg)