12,166
社区成员




[WebMethod(Description = "取得更新版本")]
//public verdata GetVer()
public verdata GetVer(verdata ver)
{
verdata vdata = ver;
if (vdata.程序名称 == "") { vdata.Code = "500"; vdata.Msg = "程序名称为空!"; return vdata; }
XmlDocument doc = new XmlDocument();
try
{
doc.Load(Server.MapPath("update.xml"));
}
catch (Exception ex)
{
vdata.Code = "500"; vdata.Msg = ex.ToString(); return vdata;
}
XmlElement root = doc.DocumentElement;
XmlNode updateNode = root.SelectSingleNode("filetype");
XmlNodeList items = updateNode.SelectNodes("item");
foreach (XmlNode item in items)
{
if (vdata.程序名称 == item.Attributes["name"].Value)
{
vdata.版本信息 = item.Attributes["version"].Value;
vdata.大小 = item.Attributes["size"].Value;
vdata.更新说明 = item.Attributes["explain"].Value;
vdata.更新时间 = item.Attributes["time"].Value;
return vdata;
}
}
//int count = int.Parse(updateNode.Attributes["count"].Value);
bool temp = false;
//for (int i = 0; i < count; i++)
//{
// XmlNode itemNode = updateNode.ChildNodes[i];
// if (vdata.程序名称 == itemNode.Attributes["name"].Value)
// {
// vdata.版本信息 = itemNode.Attributes["version"].Value;
// vdata.大小 = itemNode.Attributes["size"].Value;
// vdata.更新说明 = itemNode.Attributes["explain"].Value;
// vdata.更新时间 = itemNode.Attributes["time"].Value;
// return vdata;
// }
//}
if (!temp)
{
vdata.Code = "500";
vdata.Msg = "没有找到该文件信息!";
}
return vdata;
}
[WebMethod(Description = "在线更新软件")]
public List<verdata> GetUpdateData(string softname)
{
List<verdata> verlist = new List<verdata>();
verdata verobj = new verdata();
#region 获取该文件的
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update.xml"));
XmlElement root = doc.DocumentElement;
XmlNode updateNode = root.SelectSingleNode("filetype");
XmlNodeList items = updateNode.SelectNodes("item");
string path = "";
foreach (XmlNode item in items)
{
if (softname == item.Attributes["name"].Value)
{
path = item.Attributes["path"].Value;
break;
}
}
//int count = int.Parse(updateNode.Attributes["count"].Value);
//string path = "";
//for (int i = 0; i < count; i++)
//{
// XmlNode itemNode = updateNode.ChildNodes[i];
// if (softname == itemNode.Attributes["name"].Value)
// {
// path = itemNode.Attributes["path"].Value;
// break;
// }
//}
#endregion
#region 读取xml,返回要下载文件的list数组
XmlDocument doclist = new XmlDocument();
if (path == "") { verobj.Code = "500"; verobj.Msg = "没有该地址信息!"; verlist.Add(verobj); return verlist; }
try
{
doclist.Load(Server.MapPath(path));
}
catch (Exception ex)
{ verobj.Code = "500"; verobj.Msg = ex.ToString(); verlist.Add(verobj); return verlist; }
XmlElement rootlist = doclist.DocumentElement;
XmlNode updatalist = rootlist.SelectSingleNode("filelist");
int countlist = int.Parse(updatalist.Attributes["count"].Value);
for (int i = 0; i < countlist; i++)
{
XmlNode itemNode = updatalist.ChildNodes[i];
verobj = new verdata();
verobj.Code = "200";
verobj.程序名称 = itemNode.Attributes["name"].Value;
verobj.版本信息 = itemNode.Attributes["version"].Value;
verobj.大小 = itemNode.Attributes["size"].Value;
verobj.更新时间 = itemNode.Attributes["time"].Value;
verobj.更新说明 = itemNode.Attributes["explain"].Value;
verobj.物理路径 = itemNode.Attributes["path"].Value;
verlist.Add(verobj);
}
return verlist;
#endregion
}
/// <summary>
/// Webservice中的下载文件处理函数
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>返回文件流</returns>
[WebMethod(Description = "下载服务器站点文件,传递文件相对路径")]
public byte[] DownloadFile(string strFilePath)
{
FileStream fs = null;
//string CurrentUploadFolderPath = Server.MapPath(ConfigurationManager.AppSettings["UploadFileFolder"]);
//string CurrentUploadFilePath = CurrentUploadFolderPath + strFilePath;
//if (File.Exists(CurrentUploadFilePath))
if (File.Exists(strFilePath))
{
try
{
///打开现有文件以进行读取。
fs = File.OpenRead(strFilePath);
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = fs.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
catch (Exception ex)
{
return new byte[0];
}
finally
{
fs.Close();
}
}
else
{
return new byte[0];
}
}
#endregion
public byte[] ConvertStreamToByteBuffer(Stream s)
{
MemoryStream ms = new MemoryStream();
int b;
while ((b = s.ReadByte()) != -1)
{
ms.WriteByte((byte)b);
}
return ms.ToArray();
}
[WebMethod]
public DataTable Down(string filepath)
{
BinaryFormatter b = new BinaryFormatter();
DataTable dt = new DataTable();
dt.TableName = "m";
dt.Columns.Add("a");
if (File.Exists(filepath))
{
try
{
FileStream s = File.OpenRead(filepath);
MemoryStream m = new MemoryStream(ConvertStreamToByteBuffer(s));
dt=(DataTable)b.Deserialize(m);
s.Close();
return dt;
}
catch(Exception ex)
{
dt.Rows.Add(ex.Message); //添加一条测试数据
return dt;
}
}
else
{
dt.Rows.Add("b"); //添加一条测试数据 b
return dt;
}
}
在转datatable的时候报错:在分析完成之前就遇到流结尾。