public static void Main(string[] args)
{
DirectoryInfo dir = new DirectoryInfo(@"E:\书摘与插图\9");
Addpicture.showDir(dir);
}
namespace TaoBaoAddPicture
{
public class Addpicture
{
private static string FullName;
private static int name;
public static void showDir(DirectoryInfo dir)
{
Console.WriteLine(dir.Name);
int i = 1;
System.IO.FileInfo[] dirinfo = dir.GetFiles(); //获取文件夹下面的子文件
foreach (FileInfo info in dirinfo) //遍历子文件
{
bool status = true;
int s = info.Name.IndexOf('_');
name = Convert.ToInt32(info.Name.Substring(0, s)); //截取“_“前的名字
FullName = info.Name;
string sql = string.Format("select outer_id from TaobaoPicture");
DataSet ds = DataDrive.sqlDataConn.connGetDataSetByExecSql(sql); //查询数据库
foreach (DataRow dr in ds.Tables[0].Rows)
{
int sa = Convert.ToInt32(dr["outer_id"].ToString());
if (name == Convert.ToInt32(dr["outer_id"].ToString())) //如果数据库中存在改文件子打印”已存在“并进入下一次遍历并将状态设为false
{
Console.WriteLine(string.Format("已存在第{0}个",i));
i++;
status = false;
break;
}
}
//如果数据库中不存在该条数据则执行run()
if (status)
{
run();
Console.WriteLine(name.ToString());
Console.WriteLine(info.Name);
}
//string sql = string.Format("insert into TaobaoPicture(outer_id) values({0})", name);
//DataDrive.sqlDataConn.connGetDataSetByExecSql(sql);
}
}
上传图片
public static void run()
{
string appkey = "12188005";
string appsecret = "7e5a41417ae503f0499d071fc7c3cee4";
string url = "http://gw.api.taobao.com/router/rest";
ITopClient client = new DefaultTopClient(url, appkey, appsecret);
PictureUploadRequest req = new PictureUploadRequest();
req.ImageInputTitle = name.ToString();
req.Img = new FileItem(@"E:\书摘与插图\9\" + FullName);
req.PictureCategoryId = 303796891;
req.Title = name.ToString(); ;
PictureUploadResponse rsq = client.Execute(req);
if (rsq.IsError)
{
Console.WriteLine(rsq.ErrMsg);
}
else
{
Console.WriteLine("图片上传成功【{0}】", FullName);
}
}
}
}