• ראיון בJajah
  • ע"י: ירון778
    היי, להלן פתרון תרגיל הבית שחברת גאגא נותנת לאחר מרכז ההערכה בSQLink למשרת C# Automation בהצלחה! (הבחור שבחן אותי בתרגיל היה מתנסה ולא נעים בכלל, אבל תשפטו אותו אתם). ללא טיפול בקבצים: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Security; using System.Security.Permissions; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Security.AccessControl; namespace WindowsFormsApplication3 { public partial class Form1 : Form { private static FileSystemWatcher watcher; private static string targetFolder; public Form1() { InitializeComponent(); } [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] private void sourcePath_TextBox(object sender, EventArgs e) { if (sourcePathValidation(true)) { // Create a new FileSystemWatcher and set its properties. watcher = new FileSystemWatcher(); watcher.Path = sourcePath.Text; } } [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] private void targetPath_TextBox(object sender, EventArgs e) { bool message= true; targetFolder = targetPath.Text; if (!sourcePathValidation(false)) message = false; if (targetPathValidation(message) && !Directory.Exists(targetFolder) ) { Directory.CreateDirectory(targetFolder); } } private void stopButton_Click(object sender, EventArgs e) { if (!sourcePathValidation(false) || !targetPathValidation(false) || watcher==null) { MessageBox.Show("The program did not record anything yet, please press the start button first"); return; } if (watcher != null ) { watcher.EnableRaisingEvents = false; watcher.Dispose(); MessageBox.Show("Stop listening"); } } [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] private void startButton_Click(object sender, EventArgs e) { sourcePath_TextBox(sender, e); targetPath_TextBox(sender, e); if (!sourcePathValidation(false) || !targetPathValidation(false)) { return; } removeReadOnlyAttr(); /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // All files. watcher.Filter = "*.*"; watcher.IncludeSubdirectories = true; // Add event handlers. watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnDeleted); watcher.Renamed += new RenamedEventHandler(OnRenamed); // Begin watching. watcher.EnableRaisingEvents = true; MessageBox.Show("Start listening"); } // Define the event handlers. private static void OnCreated(object source, FileSystemEventArgs e) { } // Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) { string fileNewName; fileNewName = e.Name; string fileExt = Path.GetExtension(e.Name); //Path.GetFileName(e.Name) if (fileExt != ".TXT") { fileNewName = Path.GetFileNameWithoutExtension(e.Name) + "_" + string.Format("{0:HHmm}", DateTime.Now) + fileExt; } string fullSourcePath = Path.Combine(watcher.Path, e.Name); string fullTargetPath = Path.Combine(targetFolder, fileNewName); if (!File.Exists(fullSourcePath)) { return; } /*if (File.Exists(fullTargetPath)) { return; }*/ if (!e.Name.Contains("~") || Path.GetExtension(e.Name) != ".tmp"/* e.Name.Substring(0, 1) != "~"*/) { try { // get the file attributes for file or directory FileAttributes attr = File.GetAttributes(fullSourcePath); //detect whether its a directory or file if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { if (!e.Name.Contains("~") || Path.GetExtension(e.Name) != ".tmp") { WaitReady(fullSourcePath); } copyFolderFromSourceToTarget(fullSourcePath, fullTargetPath); MessageBox.Show("directory"); } else { WaitReady(fullSourcePath); copyFileFromSourceToTarget(fullSourcePath, fullTargetPath); } } catch (FileNotFoundException) { } } deleteAllTmpFilesInWantedFolder(targetFolder); // Specify what is done when a file is changed, created. System.Diagnostics.Debug.WriteLine("File: " + e.Name + " " + e.ChangeType); System.Diagnostics.Debug.WriteLine("File: " + fullTargetPath + " has been updated"); } // Define the event handlers. private static void OnDeleted(object source, FileSystemEventArgs e) { if (e.Name.Contains("~") /*e.Name.Substring(0, 1) == "~"*/ || Path.GetExtension(e.Name)==".tmp" || e.Name.Contains(".pptx")) { return; } // WaitReady(Path.Combine(watcher.Path,e.Name)); string fileNewName; string fileExt = Path.GetExtension(e.Name); fileNewName = Path.GetFileNameWithoutExtension(e.Name) + "_deleted" + fileExt; //string noExt = Path.GetFileNameWithoutExtension(e.Name); if (!findFileInFolder(targetFolder, e.Name, ".TXT")) { if (fileExt != ".TXT") { fileNewName = Path.GetFileNameWithoutExtension(e.Name) + "_deleted_" + string.Format("{0:HHmm}", DateTime.Now) + fileExt; } string deletedFile = Path.Combine(targetFolder, fileNewName); File.Create(deletedFile).Close(); } else { findOldFileInTargetAndChangeHisName(targetFolder, e.Name, fileNewName, ".TXT"); } deleteAllTmpFilesInWantedFolder(targetFolder); // Specify what is done when a file is deleted. System.Diagnostics.Debug.WriteLine("File: " + e.Name + " " + e.ChangeType); //System.Diagnostics.Debug.WriteLine("File: " + fullTargetPath + " has been deleted"); } private static void OnRenamed(object source, RenamedEventArgs e) { if(/*e.Name.Substring(0, 1) == "~" */e.Name.Contains("~") || Path.GetExtension(e.Name) == ".tmp") { return; } //string fileNewName; string fileNewName = e.Name; string fileExt = Path.GetExtension(e.Name); if (fileExt != ".TXT") { fileNewName = Path.GetFileNameWithoutExtension(e.Name) + "_" + string.Format("{0:HHmm}", DateTime.Now) + fileExt; } /* if (File.Exists(Path.Combine(watcher.Path, e.Name))) { WaitReady(Path.Combine(watcher.Path, e.Name)); }*/ if (findOldFileInTargetAndChangeHisName(targetFolder, e.OldName, e.Name, ".TXT")) return; string fullSourcePath = Path.Combine(watcher.Path, e.Name); string fullTargetPath = Path.Combine(targetFolder, fileNewName); try { // To copy a file to another location and // overwrite the destination file if it already exists. File.Copy(fullSourcePath, fullTargetPath, true); } catch(FileNotFoundException){} deleteAllTmpFilesInWantedFolder(targetFolder); // Specify what is done when a file is renamed. //System.Diagnostics.Debug.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); //System.Diagnostics.Debug.WriteLine("File: "+ fullTargetPath +" has been updated"); } private bool sourcePathValidation(bool message) { if (sourcePath.Text == "" || sourcePath.Text == null) { if (message) { MessageBox.Show("Source path is empty!"); } return false; } if (!isValidPath(sourcePath.Text)) { if (message) { MessageBox.Show("invalide source path"); } return false; } if (!Directory.Exists(sourcePath.Text)) { if (message) { MessageBox.Show("Source Folder does not exist."); } return false; } return true; } private bool targetPathValidation(bool message) { if (targetPath.Text == "" || targetPath.Text == null) { if (message) { MessageBox.Show("Target path is empty!"); } return false; } if (!isValidPath(targetPath.Text)) { if (message) { MessageBox.Show("invalide target path"); } return false; } if (targetPath.Text == sourcePath.Text) { if (message) { MessageBox.Show("Source and target paths are identical"); } return false; } return true; } private void removeReadOnlyAttr() { //Target FileAttributes targetAttr = File.GetAttributes(targetFolder); targetAttr = RemoveAttribute(targetAttr, FileAttributes.ReadOnly); /* | FileAttributes.Archive | FileAttributes.Compressed | FileAttributes.Device | FileAttributes.Directory);*/ File.SetAttributes(targetFolder, targetAttr); // Source FileAttributes sourceAttr = File.GetAttributes(watcher.Path); sourceAttr = RemoveAttribute(sourceAttr, FileAttributes.ReadOnly); File.SetAttributes(watcher.Path, sourceAttr); } private static bool findOldFileInTargetAndChangeHisName(string folder, string oldFileName, string newFileName,string fileExtension) { if ( /*newFileName.Substring(0, 1) == "~" */newFileName.Contains("~") || Path.GetExtension(oldFileName) == ".tmp" || Path.GetExtension(newFileName) == ".tmp") { return false; } if (Directory.Exists(folder)) { string[] files = Directory.GetFiles(folder); foreach (string s in files) { deleteTmpFile(s); string fileInDir = Path.GetFileNameWithoutExtension(s); string fileExt = Path.GetExtension(s); if (Path.GetFileNameWithoutExtension(oldFileName) == fileInDir) { string destFile = Path.Combine(folder, newFileName); try { if (!File.Exists(destFile)) { //File.Create(destFile); File.Move(s, destFile); } else { MessageBox.Show("update last deleted file with the same file name"); File.Delete(destFile); File.Move(s, destFile); } } catch (FileNotFoundException) { } catch (IOException) { } return true; } if (fileExt != fileExtension) { if (Path.GetFileNameWithoutExtension(oldFileName) == fileInDir.Substring(0, fileInDir.Length - 5)) { fileInDir = Path.GetFileNameWithoutExtension(newFileName) + "_" + string.Format("{0:HHmm}", DateTime.Now) + fileExt; string destFile = Path.Combine(folder, fileInDir); if (!File.Exists(destFile)) { try { File.Move(s, destFile); } catch (UnauthorizedAccessException) { } } else { MessageBox.Show("update last deleted file with the same file name"); try { WaitReady(destFile); File.Delete(destFile); File.Move(s, destFile); } catch(FileNotFoundException){} } return true; } } } return false; //file not found. } return false; //folder does not exist. } private static bool findFileInFolder(string folder, string FileName, string fileExtension) { if (Directory.Exists(folder)) { string[] files = Directory.GetFiles(folder); foreach (string s in files) { string fileInDir = Path.GetFileNameWithoutExtension(s); string fileExt = Path.GetExtension(s); if (Path.GetFileNameWithoutExtension(FileName) == fileInDir) { return true; } if (fileExt != fileExtension) { if (Path.GetFileNameWithoutExtension(FileName) == fileInDir.Substring(0, fileInDir.Length - 5)) { return true; } } } return false; //file not found. } return false; //folder does not exist. } private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) { return attributes & ~attributesToRemove; } private static void deleteFileFromTargetFile(string fileName) { string fullTargetPath = Path.Combine(targetFolder, fileName); // Delete a file by using File class static method... if (File.Exists(fullTargetPath)) { // Use a try block to catch IOExceptions, to // handle the case of the file already being // opened by another process. try { System.IO.File.Delete(fullTargetPath); } catch (System.IO.IOException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return; } } } private static bool isValidPath(string path) { try { path = path.Replace(@"\\", ":"); // to cancel out c:\\\\test.text string temp = Path.GetPathRoot(path); //For cases like: \text.txt if (temp.StartsWith(@"\")) return false; string pt = Path.GetFullPath(path); } catch //(Exception NotSupportedException) // catch specific exception here or not if you want { return false; } return true; } private static void copyFileFromSourceToTarget(string fullSourcePath,string fullTargetPath) { if (!File.Exists(fullTargetPath) ) { File.Create(fullTargetPath).Close(); if (File.Exists(fullSourcePath)) { try { /*using (FileStream fs = File.Create(fullSourcePath)) { fs.Close(); }*/ File.Copy(fullSourcePath, fullTargetPath, true); //File.Open(fullSourcePath, FileMode.Open, FileAccess.Write, FileShare.None).Close(); } catch (IOException) { } } } else { if (File.Exists(fullSourcePath)) { try { File.Delete(fullTargetPath); File.Copy(fullSourcePath, fullTargetPath, true); } catch (UnauthorizedAccessException) { } catch (IOException) { } } } } private static void copyFolderFromSourceToTarget(string fullSourcePath, string fullTargetPath) { if (!Directory.Exists(fullTargetPath)) { Directory.CreateDirectory(fullTargetPath); } } /// <summary> /// Waits until a file can be opened with write permission /// </summary> public static void WaitReady(string fileName) { deleteTmpFile(fileName); for (int i = 0; i < 4; i++) { try { //FileShare.ReadWrite using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { if (stream != null) { System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} ready.", fileName)); break; } } } catch (FileNotFoundException ex) { System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); } catch (IOException ex) { System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); } catch (UnauthorizedAccessException ex) { System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); } System.Threading.Thread.Sleep(3000); } //File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite).Dispose(); } private static void deleteTmpFile(string tmpFile) { try { tmpFile = Path.GetTempFileName(); // Use temporary file. } finally { if (tmpFile != null) { try { File.Delete(tmpFile); } catch (IOException) { } catch (UnauthorizedAccessException) { } } } } private static void deleteAllTmpFilesInWantedFolder(string folder) { string[] files = Directory.GetFiles(folder); foreach (string s in files) { if (Path.GetExtension(s) == ".tmp" || s.Contains("~")) { File.Delete(s); } } } private void targetPath_TextChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } } } /* if (Directory.Exists(targetFolder)) { string[] files = Directory.GetFiles(targetFolder); foreach (string s in files) { string fileInDir = Path.GetFileNameWithoutExtension(s); fileExt = Path.GetExtension(s); if (Path.GetFileNameWithoutExtension(e.OldName) == fileInDir) { string destFile = Path.Combine(targetFolder, e.Name); File.Move(s, destFile); return; } else if (fileExt!=".TXT") { if (Path.GetFileNameWithoutExtension(e.OldName) == fileInDir.Substring(0, fileInDir.Length - 5)) { fileInDir = Path.GetFileNameWithoutExtension(e.Name) + "_" + string.Format("{0:HHmm}", DateTime.Now) + fileExt; string destFile = Path.Combine(targetFolder, fileInDir); File.Move(s, destFile); return; } } } }*/
  • ע"י: 1_אורח_כללי
    אהלן לכולם, רציתי לשאול האם אתם זוכרים מה שאלו בראיון ? כאילו באת עם הקוד והסברת להם מה עשית? או שהיו עוד שאלות מסביב על השפה וידע כללי בתוכנה. תודה רבה מקווה שתענו לי (-:
  • ע"י: ירון778
    קרן1234 מה קורה? שמעי, משרדים מושקעים זה עניין יחסי. תלוי מה ראית עד היום וכאן אני אעצור. הוא לא היה קירח ולא שמן למרות שזה דווקא יכול לקלוע לרובנו. אבל אם את מתעקשת במילה אחת מתלהב. קרן1234 אני בטוח שגם נהינת מעשיית התרגיל. יאללה שיהיה בהצלחה ויקבלו אותך. תעדכני.
  • ע"י: 1_אורח_כללי
    באמת???!!! הייתי שם היום בראיון, משרדים מושקעים ונראים מצויין. המראיין שראיין אותי, היה חביב מאוד, התייחס בצורה נעימה ולא לחץ מדי. סה"כ חוויה סבירה ביותר לראיון עבודה. מי ראיין אותך, הקירח?
  • ע"י: ירון778
    היי חברה אם עדיין לא שמתם לב צירפתי את התרגיל במלואו. מה שנשאר לכם לעשות זה רק למדל למחלקות. שירלי אין לך מה להלחץ. פשוט כשאני הגעתי לשם המראיין התיחייס מאוד ביהירות קצת חיי בסרט של פזמ בצבא. זהו. באופן כללי המשרדים שלהם מוזנחים מאוד אבל ברור שזה לא אומר כלום על איכות האנשים והמוצר אותו הם מוכרים, אך למי שחשוב לו לעבוד באוירה נעימה נקיה ו"סטרטפיסטית" זה לא המקום, זה יותר בכיוון מחסן אלקטרוניקה במכללת אורט.
  • ע"י: 1_אורח_כללי
    אז ככה, בחברת SQLink היה די קליל, לא היו שאלות מקצועיות. רק סוג של תרגיל מתוך דינמיקה קבוצתית, בו מתבקשים לדרג תכונות שחשובות למהנדס פיתוח אוטומציה, כאילו אתם בצד המגייס. אחרי שכל אחד כותב לבד, צריך להחליט החלטה קולקטיבית (הסכמה של כל הקבוצה). ברוב המקומות שנתקלתי אומרים לא לבצע הערכות מספריות (הצבעה). הפעם לא אמרו לנו וזה מה שעשינו וזה עבר יחסית חלק. לאחר מכן קוראים לכל אחד מהמועמדים בנפרד לראיון אישי עם שלושה מראיינים (אחת ממשאבי אנוש, אחד ראש מחלקת פיתוח אוטומציה והשלישי ראש מחלקת QA). אם הכל הולך חלק מביאים תרגיל בית, שעליו תהיה הגנה בראיון השני. התרגיל בעצם דורש לכתוב Directory Watcher. מה זה אומר? לכתוב תוכנה שמזינים לה ספריית מקור וספריית יעד, וכאשר לוחצים "Start Listening", בכל פעם שיתווסף קובץ לתיקיית המקור, הוא יועבר לתיקיית היעד, כמובן שהתהליך הזה יסתיים ויחדל מלקרות כאשר לוחצים "Stop Listening". יש תנאי קטן נוסף: קבצי .txt יועברו כמו שהם, בעוד קבצים אחרים יועברו עם תוספת זמן ההעברה. לדוגמא הקובץ "test.pdf" כאשר יועבר השם שלו ישונה ל-"test_1235.pdf". בהצלחה לי ולבאים אחריי!!!
  • ע"י: מור_מרחב
    יש לי הזמנה לאותו מרכז הערכה ב-SQLINK ביום שני הקרוב... אשמח לדעת למה לצפות שם! תודה לעונים...
  • ע"י: שי126546
    מה השאלה חבר'ה?? תודה לעונים
  • ע"י: 1_אורח_כללי
    הי ירון, הייתי גם במרכז הערכה בSQLINK, ואני צריכה להגיע לג'גה לראיון. קצת הלחצת אותי- הם לא נחמדים שם? אתה יודע אולי מי ראיין אותך, לי קבעו ראיון עם גיל. מעבר לתרגיל שנתנו גם לי- שאלו אותך עוד משהו שם? ממש תודה שירלי
  • ע"י: ירון778
    מצורף פתרון עם התייחסות חלקית לקבצים
  • ע"י: ירון778
    מצורפים קבצים לפתרון התרגיל: