Пример #1
0
 public FileData(System.IO.FileInfo info)
 {
     CurrentLocation = info.FullName;
     Name            = info.Name;
     FirstFound      = DateTime.Now;
     Movements       = new List <FileMovement>();
     Extension       = info.Extension;
     Hash            = info.GetHashCode();
 }
Пример #2
0
        static void Main(string[] args)
        {
            #region file hashcode

            var fpath = @"D:\sysbacke450.GHO";
            var file = new FileInfo(fpath);
            var hash = file.GetHashCode();
            var MD5Hash = HashHelper.ComputeMD5(fpath);

            var copyPath = @"D:\sysbacke4501123123.GHO";
            file.CopyTo(copyPath);
            var fileC = new FileInfo(copyPath);
            var hashCopy = fileC.GetHashCode();
            var MD5HashC = HashHelper.ComputeMD5(copyPath);

            Console.WriteLine(hash);

            Console.WriteLine(MD5Hash);
            Console.WriteLine(hashCopy);
            Console.WriteLine(MD5HashC);
            Console.ReadLine();

            #endregion


            #region webapi owin self host

            //const string url = "http://localhost:8080/";
            //var startOpts = new StartOptions(url)
            //{
            //};
            ////using (WebApp.Start<Startup>(startOpts))
            ////{
            ////    Console.WriteLine("Server run at " + url + " , press Enter to exit.");
            ////    Console.ReadLine();
            ////}
            //using (WebApp.Start<Startup>(url: url))
            //{
            //    var client = new HttpClient();
            //    var response = client.GetAsync(url + "api/HomeApi/1").Result;

            //    Console.WriteLine(response);
            //    Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            //    Console.ReadLine();

            //}

            #endregion
        }
Пример #3
0
 static void Main(string[] args)
 {
     //FileInfo
     //DirectoryInfo
     FileInfo file = new FileInfo(@"C:\testfolder\inf1.txt");
     FileInfo file2 = new FileInfo(@"C:\testfolder\inf2.txt");
     // Console.WriteLine("File name " + file.Name + ", File fullname " + file.FullName);
     // Identical (^ is cout, v is printf())
     if (file.Exists)
     {
         Console.WriteLine("File name {0}, File fullname {1}", file.Name, file.FullName);
         Console.WriteLine(file.GetHashCode());
         Console.WriteLine(file2.GetHashCode());
     }
     else
     {
         Console.WriteLine("There is no such file");
     }
     Console.ReadKey();
 }
Пример #4
0
 public RealFile(FileInfo file)
 {
     _file = file;
     ID = _file.GetHashCode();
 }
Пример #5
0
        /// <summary>
        /// Compiles the source file.
        /// </summary>
        /// <param name="sourceFile">The source file.</param>
        /// <param name="source">The source.</param>
        /// <returns>Compiler results.</returns>
        private static CompilerResults CompileSourceFile(FileInfo sourceFile, string source)
        {
            CompilerResults results = null;

            string extension = sourceFile.Extension.TrimStart('.').ToUpperInvariant();

            switch (extension)
            {
                case "CS":
                    results = CSharpTestFile.Compile(source, sourceFile.GetHashCode().ToString());
                    break;

                case "VB":
                    results = VBTestFile.Compile(source, sourceFile.GetHashCode().ToString());
                    break;
            }

            return results;
        }
Пример #6
0
        private void StartDoMagic(FileInfo targetFileInfo, string targetDirectoryPath, string destinationDirectoryPath)
        {
            try
            {
                if (targetFileInfo == null)
                    throw new ArgumentNullException("targetFileInfo");

                if (destinationDirectoryPath == null)
                    throw new ArgumentNullException("destinationDirectoryPath");

                var hash = targetFileInfo.GetHashCode();
                var tempFileName = Path.Combine(PathMap.CachePath, hash + ".bin");

                if (File.Exists(tempFileName))
                    File.Delete(tempFileName);

                File.WriteAllBytes(tempFileName, new byte[]
                {
                    51
                });

                var arguments = string.Format("--create --ips \"{0}\" \"{1}\"",
                    Path.Combine(PathMap.ExecutablePath, tempFileName), targetFileInfo.FullName);

                var process = new Process
                {
                    StartInfo =
                    {
                        FileName = "flips.exe",
                        Arguments = arguments,
                        UseShellExecute = false
                    }
                };

                process.Start();
                process.WaitForExit();

                var output = targetFileInfo.DirectoryName.Replace(targetDirectoryPath + "\\", "")
                    .Replace(targetDirectoryPath, "");

                var destinationFullDirectoryPath = Path.Combine(destinationDirectoryPath, output);

                if (!Directory.Exists(destinationFullDirectoryPath))
                    Directory.CreateDirectory(destinationFullDirectoryPath);

                var ipsFileName = Path.GetFileNameWithoutExtension(targetFileInfo.FullName) + ".ips";

                var ipsFilePath = Path.Combine(targetFileInfo.DirectoryName, ipsFileName);
                var destinationFullFilePath = Path.Combine(destinationFullDirectoryPath, ipsFileName);

                if (File.Exists(destinationFullFilePath))
                    File.Delete(destinationFullFilePath);

                File.Move(ipsFilePath, destinationFullFilePath);
            }
            catch (Exception e)
            {
                _exceptions.Add(e);

                if (_exceptions.Count < MaxExceptionsCount)
                    StartDoMagic(targetFileInfo, targetDirectoryPath, destinationDirectoryPath);
            }
        }
Пример #7
0
 protected void ParseFile(FileInfo f, Hashtable h)
 {
     h[f.GetHashCode().ToString() + "/" + f.Name] = f;
 }
Пример #8
0
        /// <summary>
        /// makes any changes done while the app was not running to the remote folder
        /// For 'local to remote' mode
        /// </summary>
        /// <param name="rPath">path to remote ftpbox</param>
        /// <param name="path">path to file to be checked</param>
        /// <param name="lPath">local path to ftpbox</param>
        private void First_Check_LtR(string rPath, string path, string lPath)
        {
            FileAttributes attr = System.IO.File.GetAttributes(path);
            FileInfo fi = new FileInfo(path);
            string loc; // = fi.Name;

            loc = path.Replace(lPath, "");
            loc = loc.Replace(fi.Name, "");
            loc = loc.Replace(@"\", @"/");
            loc = noSlashes(rPath) + @"/" + noSlashes(loc);

            ftp.SetCurrentDirectory(loc);

            if ((attr & FileAttributes.Directory) != FileAttributes.Directory)
            {
                string rDir = path.Replace(lPath, "");  //remove the path to ftpBox from the path
                rDir = "/" + noSlashes(loc) + "/" + rDir;
                rDir = rDir.Replace(@"\", @"/");
                string name = fi.Name;
                rDir = rDir.Replace(name, "");
                ftp.SetCurrentDirectory(rDir);

                if (ftp.FileExists(fi.Name))
                {
                    //string rDir = path.Replace(lPath, "");    //remove the path to ftpBox from the path
                    //string name = fi.Name;                      //get the name of the file
                    //rDir = rDir.Replace(name, "");            //remove the name from the path
                    //rDir = rDir.Replace(@"\", @"/");          //replace \ with /
                    //rDir = rPath + "/" + noSlashes(rDir);     //get the full path to the new remote directory where the new file will be created

                    ftp.SetCurrentDirectory(rDir);
                    FtpFileInfo ftpfi = new FtpFileInfo(ftp, name);
                    ftpfi.Attributes.GetHashCode();

                    int rHash = ftpfi.GetHashCode();
                    int lHash = fi.GetHashCode();

                    if (rHash == lHash && ftpfi.LastWriteTime != fi.LastWriteTime)
                    {
                        ftp.RemoveFile(name);
                        ftp.PutFile(path, name);
                        if (ShowNots())
                        {
                            tray.ShowBalloonTip(100, "FTPbox", string.Format("File {0} was successfully updated.", name), ToolTipIcon.Info);
                        }
                    }
                }
                else
                {
                    //string name = fi.Name;                  //get the name of the file
                    /*
                    rDir = rDir.Replace(name, "");          //remove the name from the path
                    rDir = rDir.Replace(@"\", @"/");        //replace \ with /
                    rDir = rPath + "/" + noSlashes(rDir);   //get the full path to the new remote directory where the new file will be created */

                    ftp.PutFile(path, name);
                    if (ShowNots())
                    {
                        tray.ShowBalloonTip(100, "FTPbox", string.Format("File {0} was successfully uploaded.", name), ToolTipIcon.Info);
                    }

                }
            }

            else if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
            {
                string rDir = path.Replace(lPath, "");  //remove the path to ftpBox from the path
                string name = fi.Name;                  //get the name of the file
                rDir = "/" + noSlashes(loc) + "/" + rDir;
                ftp.SetCurrentDirectory(rDir);

                if (!ftp.DirectoryExists(name))
                {
                    /*
                    rDir = rDir.Replace(name, "");          //remove the name from the path
                    rDir = rDir.Replace(@"\", @"/");        //replace \ with /
                    rDir = rPath + "/" + noSlashes(rDir);   //get the full path to the new remote directory where the new file will be created */

                    ftp.CreateDirectory(name);
                    if (ShowNots())
                    {
                        tray.ShowBalloonTip(100, "FTPbox", string.Format("Folder {0} was successfully created.", name), ToolTipIcon.Info);
                    }
                }
            }
        }
Пример #9
0
        private void Run(CancellationToken ct)
        {
            lock (lockObj)
            {
                var extentions  = Extentions.ToList();
                var folderItems = FolderItems.ToList();

                // Validate
                OnInfoLogEvent(new LogEventArgs("パラメータチェック"));
                if (!System.IO.Directory.Exists(Path.GetFullPath(this.FromPath)))
                {
                    OnErrorLogEvent(new LogEventArgs("参照元パスが有効ではありません。"));
                    return;
                }

                if (!System.IO.Directory.Exists(Path.GetFullPath(this.ToPath)))
                {
                    OnErrorLogEvent(new LogEventArgs("参照先パスが有効ではありません。"));
                    return;
                }

                // 非同期キャンセル処理
                if (IsCanceled(ct))
                {
                    return;
                }

                // Get File List
                OnInfoLogEvent(new LogEventArgs("参照元ファイル内容取得"));
                var tempFileList = Directory.EnumerateFiles(Path.GetFullPath(this.FromPath), "*", SearchOption.AllDirectories);
                var fileList     = new List <string>();
                if (extentions.Count > 0)
                {
                    foreach (var ext in extentions)
                    {
                        OnInfoLogEvent(new LogEventArgs($"拡張子 [{ext}] のリスト取得"));
                        fileList.AddRange(tempFileList.Where(x => x.EndsWith("." + ext, StringComparison.CurrentCultureIgnoreCase)));
                    }
                }
                else
                {
                    OnInfoLogEvent(new LogEventArgs("全拡張子のリスト取得"));
                    fileList.AddRange(tempFileList);
                }

                // 非同期キャンセル処理
                if (IsCanceled(ct))
                {
                    return;
                }

                // Create PhotoFiles
                OnInfoLogEvent(new LogEventArgs("ファイルの情報取得"));
                List <PhotoFile> photoFiles = fileList.Select(x => new PhotoFile(x)).ToList();

                // 非同期キャンセル処理
                if (IsCanceled(ct))
                {
                    return;
                }

                // TODO Sort File List
                photoFiles = photoFiles.OrderBy(x => x.CreationTime).ToList();

                // 非同期キャンセル処理
                if (IsCanceled(ct))
                {
                    return;
                }

                // フォルダ作成してファイル移動
                foreach (var pf in photoFiles)
                {
                    // 非同期キャンセル処理
                    if (IsCanceled(ct))
                    {
                        return;
                    }
                    var dir = toPath;

                    if (string.IsNullOrEmpty(pf.FileName))
                    {
                        continue;
                    }

                    foreach (var fi in folderItems)
                    {
                        switch (fi)
                        {
                        case FolderItem.Extension:
                            dir = Path.Combine(dir, pf.Extension.Substring(1).ToLower());
                            break;

                        case FolderItem.Year:
                            dir = Path.Combine(dir, pf.LastWriteTime.ToString("yyyy"));
                            break;

                        case FolderItem.YearMonth:
                            dir = Path.Combine(dir, pf.LastWriteTime.ToString("yyyyMM"));
                            break;

                        case FolderItem.YearMonthDay:
                            dir = Path.Combine(dir, pf.LastWriteTime.ToString("yyyyMMdd"));
                            break;

                        case FolderItem.Month:
                            dir = Path.Combine(dir, pf.LastWriteTime.ToString("MM"));
                            break;

                        case FolderItem.MonthDay:
                            dir = Path.Combine(dir, pf.LastWriteTime.ToString("MMdd"));
                            break;

                        case FolderItem.Day:
                            dir = Path.Combine(dir, pf.LastWriteTime.ToString("dd"));
                            break;
                        }

                        if (!Directory.Exists(dir))
                        {
                            OnInfoLogEvent(new LogEventArgs($"ディレクトリ [{dir}] の作成"));
                            try
                            {
                                Directory.CreateDirectory(dir);
                            }
                            catch (Exception e)
                            {
                                OnErrorLogEvent(new LogEventArgs($"ディレクトリ [{dir}] の作成に失敗しました : {e.Message}"));
                                return;
                            }
                        }
                    }

                    // 非同期キャンセル処理
                    if (IsCanceled(ct))
                    {
                        return;
                    }

                    var fileInfo   = new System.IO.FileInfo(pf.FullName);
                    var fileExists = true;
                    var fileName   = pf.FileName;
                    var inc        = 0;

                    while (fileExists)
                    {
                        if (File.Exists(Path.Combine(dir, fileName)))
                        {
                            // 同一ファイルが存在する場合
                            var toFileInfo = new System.IO.FileInfo(Path.Combine(dir, fileName));

                            // 同一ハッシュの場合はコピーしない
                            if (fileInfo.GetHashCode() == toFileInfo.GetHashCode())
                            {
                                OnInfoLogEvent(new LogEventArgs($"ファイル [{Path.Combine(dir, fileName)}] がすでに存在しましたが、ハッシュ値が一致したため処理対象外とします"));
                                break;
                            }

                            OnInfoLogEvent(new LogEventArgs($"ファイル [{Path.Combine(dir, fileName)}] がすでに存在するため、ファイル名を変更します"));
                            // ファイル名を設定して再判定
                            inc++;
                            fileName = pf.FileNameWithoutExtention + inc.ToString();
                            fileName = fileName + pf.Extension;
                        }
                        else
                        {
                            fileExists = false;
                        }
                    }

                    if (fileExists)
                    {
                        continue;
                    }

                    // 非同期キャンセル処理
                    if (IsCanceled(ct))
                    {
                        return;
                    }

                    try
                    {
                        if (IsCopy)
                        {
                            OnInfoLogEvent(new LogEventArgs($"ファイル [{fileInfo.FullName}] を [{Path.Combine(dir, fileName)}] にコピーします"));
                            fileInfo.CopyTo(Path.Combine(dir, fileName));
                        }
                        else
                        {
                            OnInfoLogEvent(new LogEventArgs($"ファイル [{fileInfo.FullName}] を [{Path.Combine(dir, fileName)}] に移動します"));
                            fileInfo.MoveTo(Path.Combine(dir, fileName));
                        }
                    }
                    catch (Exception e)
                    {
                        OnErrorLogEvent(new LogEventArgs($"ファイル [{dir}] の操作に失敗しました : {e.Message}"));
                    }
                }
            }
        }