How To Move Image from One location To Another Location using C#,Asp.net MVC


How To Move Image from One location To Another Location using C#




using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;

namespace myProject
{
    public static class Utils

    {

public static void MoveImages(string sessPath, string dirPath, int fileLimitToMove = 25)
        {
            try
            {
                DirectoryInfo sessionDirInfo = new DirectoryInfo(sessPath);
                if (sessionDirInfo.Exists)
                {
                    DirectoryInfo desDirInfo = new DirectoryInfo(dirPath);
                    if (!desDirInfo.Exists)
                        desDirInfo.Create();

                    int fileLimitIndex = 0;
                    foreach (var file in sessionDirInfo.GetFiles())
                    {
                        if (fileLimitIndex >= fileLimitToMove)
                        {
                            break;
                        }
                        string fileName = Path.GetFileName(file.Name);
                        if (File.Exists(Path.Combine(dirPath + "\\", fileName)))
                        {
                            File.Delete(Path.Combine(dirPath + "\\", fileName));
                        }
                        System.IO.File.Move(sessPath + "\\" + fileName, Path.Combine(dirPath + "\\", fileName));
                        fileLimitIndex = fileLimitIndex + 1;
                    }
                    Directory.Delete(sessPath, true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
   }
}

No comments:

Post a comment

Thank You For Your Great Contribution

Featured Post

how to find duplicate item from array using multiple object keys in angular or javascript

 how to find duplicate item from array using multiple object keys There is a way to find based of multiple key find duplicate records in ang...

Popular Posts