0

I need to open file from folder by part of name. I have files in one folder for example all files are in c:\temp\ Names of files are in format id_yyyyMMdd.pdf, where id is some number, id of file , and id is always unique, yyyyMMdd are date in format year,month,day. Now in program i get id and i need to find file with that id, then check date when file is created , and if there are new version of file(blob in database) i need to write this new file to disk and than open it, if this is last version of file i need just open.(i have PDFs like blob, and i need to check is on hard disk last version file.) I know how to write pdf from blob to file i cannot find way to open(or find) file with part of name , and thaen get second part(date) to compare it.

Any suggestions?

Sample: on hard disk i have files:

  • c:\temp\12345_20140508.pdf
  • c:\temp\12346_20140508.pdf

In database table i have

  • id(12345) date(08-MAY-2014) and pdf1 file like blob.
  • id(12346) date(10-MAY-2014) and pdf2 file like blob.

Now in program I select id "12345" because dates are same i need to open file 12345_20140508.pdf When i get select id 12346 i need first get file file from database because date in database is newer than date on hard disk, remove file 12346_20140508.pdf and save new file like 12346_20140510.pdf and than open.

If it is need i can change format of name.

2 Answers 2

1

I supposed the main problem you met is "how to find the newest file by id". So may be you can try this code. It can find the last created file start with some id. Once you get the file, I thought other things won't be a problem to you.

var directoryInfo = new DirectoryInfo(@"c:\temp");
var latestFile = directoryInfo.GetFiles()
                              .Where(f => f.Name.StartsWith("12345_"))
                              .OrderByDescending(f => f.CreationTime)
                              .FirstOrDefault();
0
1

what you should do is:

string searchPattern = "what you want to open";

File.Open(Directory.GetFiles(@"c:\", searchPattern + "*").First());

That will open the file in the c# sense,

however to open in an app use

Process.Start("notepad.exe", Directory.GetFiles(@"c:\", searchPattern + "*").First());

if you want to open them all wrap this in a foreach

foreach (var file in Directory.GetFiles(@"c:\", searchPattern + "*"))
{
    //do stuff
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.