←Select platform

InsertFile Method

Adds a DICOM file to the Directory.
public void InsertFile( 
   string fileName 
- (BOOL)insertFile:(nullable NSString *)fileName error:(NSError **)error; 
void InsertFile(  
   String^ fileName 


The name of the DICOM file to be added to the DICOM Directory.


Before using this method to add a DICOM file to the Directory, a destination folder must have been specified, either at the construction time or by using the Reset method. Otherwise, the method will fail. The DICOM file to be added must reside in the destination folder or in a subfolder of the destination folder. Otherwise, the method will fail and the file will not be added to the directory. For example, if the destination folder is specified as "C:\DICOM Images\", then the DICOM file "C:\DICOM Images\Patient 1\Brain" can be added to the Directory, while the DICOM file "C:\Images\Knee" cannot be added. Please notice that the filename passed to the method must specify the absolute path of the file, not the relative one.

If fileName is set to a null reference (Nothing in VB), the method will add all the DICOM files contained in the destination folder to the DICOM Directory. In this case, the OnInsertFile callback method will be called for each file, so that the progress can be monitored. To add all the DICOM files in the destination folder and all the DICOM files in the subfolders of the destination folder, use the Options property to set IncludeSubfolders to true, and then call this method with fileName set to a null reference.

If fileName is not null, the OnInsertFile callback method will not be called.

using Leadtools; 
using Leadtools.Dicom; 
public class MyDicomDir : DicomDir 
   public MyDicomDir(string destinationFolder, string path) : base(destinationFolder, path) { } 
   public override DicomDirInsertFileCommand OnInsertFile(string fileName, DicomDataSet ds, DicomDirInsertFileStatus status, DicomExceptionCode code) 
      if (status == DicomDirInsertFileStatus.PreAdd) 
         string msg = string.Format("About to add the file '{0}'{1}{1}Proceed?", fileName, Environment.NewLine); 
         return DicomDirInsertFileCommand.Continue; 
      else if (status == DicomDirInsertFileStatus.Success) 
         string msg = string.Format("The file '{0}' has been added successfully.", fileName); 
      return DicomDirInsertFileCommand.Continue; 
public void CreateDicomDirectory() 
   //Make sure to initialize the DICOM engine, this needs to be done only once  
   //In the whole application 
   using (MyDicomDir dicomDir = new MyDicomDir(null, null)) 
      bool autoSearch = true; 
      // Set the destination folder 
      dicomDir.Reset(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1")); 
      // Set the File-set Identifier 
      dicomDir.SetFileSetId("SOME ID"); 
      // Set some options 
      DicomDirOptions options = dicomDir.Options; 
      options.IncludeSubfolders = true; 
      options.InsertIconImageSequence = false; 
      options.RejectInvalidFileId = true; 
      dicomDir.Options = options; 
      // Set the File-set descriptor file 
      string path = Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\README"); 
      dicomDir.SetDescriptorFile(path, null); 
      if (autoSearch) 
         catch (Exception ex) 
            Console.WriteLine($"{ex.GetType().ToString()} {ex.Message}"); 
         // Load a Data Set and add it to the Directory 
         using (DicomDataSet ds = new DicomDataSet()) 
            ds.Load(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\Image1"), DicomDataSetLoadFlags.None); 
            dicomDir.InsertDataSet(ds, Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE1")); 
         // Add some DICOM files to the Directory 
         dicomDir.InsertFile(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE2")); 
         dicomDir.InsertFile(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE3")); 
         dicomDir.InsertFile(Path.Combine(LEAD_VARS.ImagesDir, @"TEMP\DCMTEST\PATIENT1\IMAGE4")); 
      // Test the DataSet 
      DicomElement first = dicomDir.DataSet.FindFirstElement(null, DicomTag.PatientID, false); 
      // Save the DICOMDIR File 
      //We can now call something like this to call the DICOMDIR back 
      //dicomDir.Load(LeadtoolsExamples.Common.ImagesPath.Path + @"TEMP\DCMTEST\PATIENT1\DICOMDIR",DicomDataSetLoadFlags.LoadAndClose); 
static class LEAD_VARS 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 

Target Platforms

Help Version 22.0.2023.1.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Dicom Assembly