本文整理匯總了C#中System.IO.DirectoryInfo.GetImageFiles方法的典型用法代碼示例。如果您正苦於以下問題:C# DirectoryInfo.GetImageFiles方法的具體用法?C# DirectoryInfo.GetImageFiles怎麽用?C# DirectoryInfo.GetImageFiles使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.IO.DirectoryInfo
的用法示例。
在下文中一共展示了DirectoryInfo.GetImageFiles方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: CheckProductImages
public void CheckProductImages(IList<Product> productsNotExistInDb, string ImageFolder,
out IList<Product> productsHasPicture
, out IList<Product> productsNotHasPicture
, out IList<FileInfo> imagesHasProduct
, out IList<FileInfo> imagesHasNotProduct)
{
DirectoryInfo dir = new DirectoryInfo(ImageFolder);
FileInfo[] images = dir.GetImageFiles(SearchOption.AllDirectories).ToArray<FileInfo>();// dirImage.GetFiles();
productsHasPicture = new List<Product>();
productsNotHasPicture = new List<Product>();
imagesHasProduct = new List<FileInfo>();
imagesHasNotProduct = new List<FileInfo>();
//寫一個通用類,比較兩個序列,返回匹配結果.
//Compare<T1,T2> T1和T2需要實現他們兩者比較的接口
foreach (Product p in productsNotExistInDb)
{
bool productHasImage = false;
Console.WriteLine("productModel:" + p.ModelNumber);
foreach (FileInfo image in images)
{
string imageName = StringHelper.ReplaceSpace(Path.GetFileNameWithoutExtension(image.Name));
Console.Write("imageName:" + imageName);
if (imageName.Equals(StringHelper.ReplaceSpace(p.ModelNumber), StringComparison.OrdinalIgnoreCase))
{
//找到型號相同的圖片
p.UpdateImageList(image.FullName, WebProductImagesPath);
productsHasPicture.Add(p);
imagesHasProduct.Add(image);
productHasImage = true;
break;
}
}
if (!productHasImage)
{
productsNotHasPicture.Add(p);
}
}
foreach (FileInfo f in images)
{
bool imageHasProduct = false;
foreach (FileInfo f2 in imagesHasProduct)
{
if (f.Name.Equals(f2.Name))
{
imageHasProduct = true;
break;
}
}
if (!imageHasProduct)
{
imagesHasNotProduct.Add(f);
}
}
}
示例2: CheckSingleFolder2Test
private void CheckSingleFolder2Test(
string folderName
, int amountProductsHasPicture
, int amountProductNotHasPicture
, int amountProductExistsInDb
, int amountImagesHasNotProduct
, bool needCheckDataBase
, DALProduct dalProduct
, DALSupplier dalSupplier
, IFormatSerialNoPersistent ifsp
, DalBase<Product> dalBaseProduct)
{
string folderFullPath = Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\" + folderName + "\\";
SingleFolderImport checker = new SingleFolderImport(folderFullPath);
BizProduct bizP = new BizProduct();
bizP.DalProduct = dalProduct;
bizP.DalBase = dalBaseProduct;
BizSupplier bizS = new BizSupplier();
bizS.DalSupplier = dalSupplier;
bizP.DalSupplier = dalSupplier;
checker.NeedCheckWithDB = needCheckDataBase;
checker.Import(bizP, bizS, ifsp);
IList<Product> productsHasPicture = checker.ProductsPassedDBCheck
, productsNotHasPicture = checker.ProductsNotHasImage
, productsExistedInDB = checker.ProductsExistedInDB;
IList<FileInfo> imagesHasProduct = checker.ImagesHasProduct
, imagesHasNotProduct = checker.ImagesNotHasProduct;
// Assert.AreEqual("Success", FormatChecker.Check(folderContainsExcelAndImages));
Assert.AreEqual(amountProductsHasPicture, checker.ProductsPassedDBCheck.Count);
Assert.AreEqual(amountProductNotHasPicture, checker.ProductsNotHasImage.Count);
Assert.AreEqual(amountProductExistsInDb, checker.ProductsExistedInDB.Count);
Assert.AreEqual(amountImagesHasNotProduct, checker.ImagesNotHasProduct.Count);
DateTime beginSaveResult = DateTime.Now;
string saveFolder = Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\檢測結果\\";
string saveFolderOfSupplier;
if (productsHasPicture.Count > 0) saveFolderOfSupplier = bizS.GetByCode(productsHasPicture[0].SupplierCode).Name;
else if (productsNotHasPicture.Count > 0) saveFolderOfSupplier = bizS.GetByCode(productsNotHasPicture[0].SupplierCode).Name;
else throw new Exception();
DirectoryInfo dirOfSavedSupplier = new DirectoryInfo(saveFolder + "合格數據\\" + saveFolderOfSupplier + "\\");
if (dirOfSavedSupplier.Exists)
{
dirOfSavedSupplier.Delete(true);
}
string supplierName = string.Empty;
if (productsExistedInDB.Count > 0) supplierName = bizS.GetByCode(productsExistedInDB[0].SupplierCode).Name;
else if (productsHasPicture.Count > 0) supplierName = bizS.GetByCode(productsHasPicture[0].SupplierCode).Name;
else if (productsNotHasPicture.Count > 0) supplierName = bizS.GetByCode(productsNotHasPicture[0].SupplierCode).Name;
else
{
return;
}
supplierName = StringHelper.ReplaceInvalidChaInFileName(supplierName, string.Empty);
checker.HandlerCheckResult(
supplierName,
saveFolder,
WebProductImagesPath
);
Assert.AreEqual(productsHasPicture.Count, dirOfSavedSupplier.GetImageFiles().ToArray().Length);
Console.WriteLine("Time Cost CheckImage:" + (DateTime.Now - beginSaveResult).TotalSeconds);
}
示例3: CheckSingleFolderTest
/// <summary>
///
/// </summary>
/// <param name="folderName">包含excel和對應產品圖片的文件夾</param>
/// <param name="amountProductsHasPicture_NotExitsted">可供導入的產品(沒有導入過,有圖片)</param>
/// <param name="amountProductNotHasPicture_OrHasExisted">不合格產品(已導入,或 沒圖片</param>
/// <param name="amountProductExistsInDb_HasExisted">不合格產品(已導入)</param>
/// <param name="amountImagesHasNotProduct_OrHasExisted">沒有產品信息的圖片</param>
/// <param name="needCheckDataBase">是否從數據庫查詢產品是否存在</param>
/// <param name="dalProduct">供mock,判斷是否存在的方法</param>
/// <param name="dalSupplier">供mock,獲取供應商的方法.</param>
private void CheckSingleFolderTest(
string folderName
, int amountProductsHasPicture
, int amountProductNotHasPicture
, int amountProductExistsInDb
, int amountImagesHasNotProduct
, bool needCheckDataBase
, DALProduct dalProduct
, DALSupplier dalSupplier)
{
IList<Product> productsHasPicture, productsNotHasPicture, productsExistedInDB;
IList<FileInfo> imagesHasProduct, imagesHasNotProduct;
string folderFullPath = Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\" + folderName + "\\";
ProductImportor checker = new ProductImportor();
checker.BizProduct.DalBase = dalProduct;
checker.BizProduct.DalSupplier = dalSupplier;
checker.CheckWithDatabase = needCheckDataBase;
checker.CheckSingleFolder(folderFullPath
, out productsHasPicture
, out productsNotHasPicture
, out productsExistedInDB
, out imagesHasProduct
, out imagesHasNotProduct);
// Assert.AreEqual("Success", FormatChecker.Check(folderContainsExcelAndImages));
Assert.AreEqual(amountProductsHasPicture, productsHasPicture.Count);
Assert.AreEqual(amountProductNotHasPicture, productsNotHasPicture.Count);
Assert.AreEqual(amountProductExistsInDb, productsExistedInDB.Count);
Assert.AreEqual(amountImagesHasNotProduct, imagesHasNotProduct.Count);
DateTime beginSaveResult = DateTime.Now;
string saveFolder=Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\檢測結果\\";
string saveFolderOfSupplier;
if (productsHasPicture.Count > 0) saveFolderOfSupplier = productsHasPicture[0].SupplierName;
else if (productsNotHasPicture.Count > 0) saveFolderOfSupplier = productsNotHasPicture[0].SupplierName;
else throw new Exception();
DirectoryInfo dirOfSavedSupplier = new DirectoryInfo(saveFolder + "合格數據\\" + saveFolderOfSupplier+"\\");
if (dirOfSavedSupplier.Exists)
{
dirOfSavedSupplier.Delete(true);
}
string supplierName = string.Empty;
if (productsExistedInDB.Count > 0) supplierName = productsExistedInDB[0].SupplierName;
else if (productsHasPicture.Count > 0) supplierName = productsHasPicture[0].SupplierName;
else if (productsNotHasPicture.Count > 0) supplierName = productsNotHasPicture[0].SupplierName;
else
{
return;
}
supplierName = StringHelper.ReplaceInvalidChaInFileName(supplierName, string.Empty);
checker.HandlerCheckResult(
supplierName,
productsHasPicture
, productsNotHasPicture
, productsExistedInDB
, imagesHasProduct
, imagesHasNotProduct
, saveFolder);
Assert.AreEqual(productsHasPicture.Count, dirOfSavedSupplier.GetImageFiles().ToArray().Length);
Console.WriteLine("Time Cost CheckImage:" + (DateTime.Now - beginSaveResult).TotalSeconds);
}
示例4: ImportImage
/// <summary>
/// 掃描指定文件夾,根絕文件夾和圖片名稱提取圖片信息
/// <param name="targetPath">拷貝的目標地址,應該為網站的虛擬目錄的物理路徑</param>
/// </summary>
public IList<ImageInfo> ImportImage(string folderPath, string targetPath, out string message)
{
StringBuilder sb = new StringBuilder();
IList<ImageInfo> images = new List<ImageInfo>();
DirectoryInfo dir = new DirectoryInfo(folderPath);
DirectoryInfo[] supplierDirs = dir.GetDirectories();
//foreach (DirectoryInfo dirSupplier in supplierDirs)
//{
// string supplierNameOfFolder = dirSupplier.Name;
// Supplier s = dalSupplier.GetOneByName(supplierNameOfFolder);
// if (s == null)
// {
// string errmsg = "沒有找到與文件夾同名的供應商名稱,請核查." + supplierNameOfFolder;
// NLibrary.NLogger.Logger.Debug(errmsg);
// sb.AppendLine(errmsg);
// continue;
// }
FileInfo[] imageFiles = dir.GetImageFiles().ToArray();
if (imageFiles.Length == 0)
{
string errmsg = "文件夾內沒有圖片:" + dir.FullName;
NLibrary.NLogger.Logger.Debug(errmsg);
sb.AppendLine(errmsg);
}
//本次操作中出現過的供應商
IList<Supplier> supplierList = new List<Supplier>();
foreach (FileInfo imageFile in imageFiles)
{
string supplierNameOfFolder = imageFile.Directory.Name;
Supplier s = supplierList.SingleOrDefault(x => x.Name == supplierNameOfFolder || x.EnglishName == supplierNameOfFolder);
if (s == null)
{
s = DalSupplier.GetOneByName(supplierNameOfFolder);
if (s == null)
{
string errmsg = "沒有找到與文件夾同名的供應商名稱,請核查." + supplierNameOfFolder;
NLibrary.NLogger.Logger.Debug(errmsg);
sb.AppendLine(errmsg);
continue;
}
if (!supplierList.Contains(s))
{
supplierList.Add(s);
}
}
IList<Product> ProductsOfSupplier = DalProduct.GetListBySupplierCode(s.Code);
if (ProductsOfSupplier.Count == 0)
{
string errmsg = "沒有屬於該供應商的產品,請導入對應的報價單 或者 核實文件夾名稱與供應商名稱的一致性." + supplierNameOfFolder;
NLibrary.NLogger.Logger.Debug(errmsg);
sb.AppendLine(errmsg);
continue;
}
//讀取型號名稱
string modelNumber =StringHelper.ToDBC(Path.GetFileNameWithoutExtension(imageFile.Name).Trim());
Product p = null;//= dalProduct.GetOneByModelNumberAndSupplier(modelNumber, dirSupplier.Name);
IList<Product> productSupplierAndModel = ProductsOfSupplier.Where(
x => StringHelper.ToDBC(x.ModelNumber.Trim().Replace("\n", "")).ToLower() == modelNumber.ToLower()).ToList();
if (productSupplierAndModel.Count == 0)
{
string errmsg = "該圖片沒有對應的產品信息:" + supplierNameOfFolder + "," + imageFile.Name;
NLibrary.NLogger.Logger.Debug(errmsg);
sb.AppendLine(errmsg);
// NLibrary.NLogger.Logger.Debug("沒有找到對應的圖片.供應商:" + dirSupplier.FullName+",型號:"+modelNumber);
continue;
}
else if (productSupplierAndModel.Count > 1)
{
string errmsg = "該圖片對應多條產品信息,:" + supplierNameOfFolder + "," + imageFile.Name;
NLibrary.NLogger.Logger.Debug(errmsg);
sb.AppendLine(errmsg);
}
//拷貝圖片 到 對應文件夾
p = productSupplierAndModel[0];
p.UpdateImageList(imageFile.FullName, targetPath + "\\");
DalProduct.Update(p);
ImageInfo ii = new ImageInfo();
ii.ImagePath = imageFile.FullName;
ii.ModelNumber = modelNumber;
ii.SupplierName = s.Name;
images.Add(ii);
}
message = sb.ToString();
return images;
}