using System.ComponentModel;
using System.Web.Services;
using System.IO;
using System;
namespace WebServiceFileDirectory
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://volnet.cnblogs.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class ServiceFileDirectory : System.Web.Services.WebService
{
private static readonly string path = System.Configuration.ConfigurationManager.AppSettings["directory"];
[WebMethod]
public string[] GetDirectories()
{
if (MakeSurePath() == true)
{
return Directory.GetDirectories(path);
}
return null;
}
[WebMethod]
public string[] GetFiles()
{
if (MakeSurePath() == true)
{
return Directory.GetFiles(path);
}
return null;
}
private bool? isCorrectPath = null;
/// <summary>
/// 确保路径正确
/// </summary>
/// <returns></returns>
private bool? MakeSurePath()
{
try
{
if (isCorrectPath.HasValue == false)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
isCorrectPath = true;
}
}
catch
{
isCorrectPath = false;
}
finally
{
}
return isCorrectPath;
}
}
}
//Tester:
protected void btnGetFiles_Click(object sender, EventArgs e)
{
FileSystem.ServiceFileDirectory sf = new WebApplicationTester.FileSystem.ServiceFileDirectory();
string[] files = sf.GetFiles();
string result= string.Empty;
foreach (string str in files)
{
result += str + "<BR>";
}
this.Response.Write(result);
}