Saturday 15 September 2012

C# File - Read files from desktop and write them to a specific file -


I created the System.Timers.Timer object with a gap of 5000 ms. On this timer's has expired event, I am searching for new PDF files that were shown on the desktop. If I have new PDF files, then I add them to a specific file, but my program captures this error: This process can not be used by 'C: \ Users \ admin \ Desktop \ startupfiles DAT' file Because it is being used by another process here is my code:

  Private Readonly string filename = Application.StartupPath + @ "\ StartupFiles.dat"; Private Redonline string source directory = environment Gatefolder Path (environment specialfolder.desktop); Zero timer check_aleps (object sender, System.Timers.ElapsedEventArgs E) {try (if.file.Exists (fileName)) File.Create (fileName); String [] PDFiles = Directory.GetFiles (sourceDirectory, "* .pdf", SearchOption.TopDirectoryOnly); String [] textFile = File.ReadAllLines (fileName); Bull exists; String addText = string.Empty; Foreach (string in pdfiles) / file with file from file, check file names from variabile folder {exist = false; Foreach (String C in text file) {if (string.Comparare (s, c) == 0) {exist = true; break; }} If (! Exists) {addText + = s + '\ n'; }} If (! String.IsNullOrEmpty (addText)) // If a new PDF is appealing to the desktop, then file it using {{StreamWriter sw = file.AppendText (fileName)} {sw.Write (addText) Save; }}} Hold (Exception Pre) {Message Box. Show (ex.Message); }}   

Maybe I have readAllLines and File.AppendText

@charqus, it should work

  if (! File. Exists (fileName) File.Create (File Name) .Dispose (); String [] PDFiles = Directory.GetFiles (sourceDirectory, "* .pdf", SearchOption.TopDirectoryOnly); & Lt; String & gt; FileList = new list & lt; String & gt; (); (FileStream FS = New filestream (using FileName, FileMode, File Access Reid)) (Binary Render R = New Binary Reader (FS)) {fileList.Add (r.ReadString ()); }} String [] textFile = fileList.ToArray ();   

Calling the Dispos method ensures that all the resources are released properly.

No comments:

Post a Comment