以单个工作项示例:工作项ID可以从数据库或者其他文件中读取
-----------
using System.IO;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net;
namespace TestFileStream
{
class Program
{
static void Main(string[] args)
{
SqlConnection sqlConnection = new SqlConnection(
"Integrated Security=true;server=数据库服务器名称;database=对应的tfs数据库");
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
string sql = "SELECT * FROM WorkItemFiles WHERE id='工作项ID' ";
SqlDataAdapter sda = new SqlDataAdapter(sql, sqlConnection);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables.Count > 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
string fileId = row["ExtID"].ToString();
string fileName = row["OriginalName"].ToString();
DownLoadFile("工作项ID", fileName, fileId);
}
}
}
public static void DownLoadFile(string WorkItemId,string fileName, string fileId)
{
//手工分析项目中的附件链接
string filePath = "http://*****/attachfilehandler.ashx?fileid=" + fileId;
WebRequest req = WebRequest.Create(filePath);
WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
wc.Credentials = new NetworkCredential("帐号", "密码", "域名");
wc.DownloadData(filePath);
Stream str = wc.OpenRead(filePath);
// 初始化一个缓存区
byte[] buffer = new byte[1024];
int read = 0;
int block;
while ((block = str.Read(buffer, read, buffer.Length - read)) > 0)
{
read += block;
if (read == buffer.Length)
{
int nextByte = str.ReadByte();
if (nextByte == -1)
{
break;
}
byte[] newBuf = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuf, buffer.Length);
newBuf[read] = (byte)nextByte;
buffer = newBuf;
read++;
}
}
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
//文件写入本地指定的文件夹,文件名称:"工作项ID_文件ID_文件名"
FileStream fstr = new FileStream("f:\\Test\\" + WorkItemId + "_" + fileId + "_" + fileName, FileMode.Create, FileAccess.Write);
fstr.Write(ret, 0, ret.Length);
}
}
}
VSTS,SVN,TFS,SqlServer,版本控制,源码管理,bug管理,需求管理,TFS2010 交流群
群号:197992930 #点击加群#19 71999293019799