After a long time of being off the blog I'm returning with a nice thingie:
A web service to delete files from the server.
This version is the most basic one. Meaning: it doesn't do any checks for permissions, no massive error handling and almost nothing else. That's the reason it works.
So to the code. First thing is the deletion procedure, DeleteFile, which returns true upon success and false upon failure. The err is used in order to return a better description of the error when failing to do the job.
private static bool DeleteFile(string Filename, out string err)
{
err = "";
try
{
if (System.IO.File.Exists(Filename))
{
System.IO.File.Delete(Filename);
return true;
}
else
{
err = "file does not exist!";
return false;
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
}
And now for the access point the web method (note the header)
[WebMethod]
public System.Xml.XmlDocument DeleteMedias(System.Xml.XmlDocument doc)
{
foreach (System.Xml.XmlNode nd in doc.SelectNodes("/files/file"))
{
if (nd.SelectSingleNode("filename") != null)
{
System.Xml.XmlNode result = doc.CreateNode(System.Xml.XmlNodeType.Element, "result", "");
string err;
if (DeleteFile(nd.SelectSingleNode("filename").InnerText, out err))
{
result.InnerText = "success";
}
else
{
result.InnerText = "error: " + err;
}
nd.AppendChild(result);
}
}
return doc;
}
It's almost self-explaining, pass an XML in this format:
<files>
<file>
<filename>
c:\xxx\yyy.zzz
</filename>
</file>
</files>
and the reply would be:
<files>
<file>
<filename>
c:\xxx\yyy.zzz
</filename>
<result>
success
</result>
</file>
</files>
This is no rocket science, actually this is a 15 minutes job that was done in order to replace a way too heavy code block.
Thursday, March 13, 2008
Subscribe to:
Posts (Atom)