Also the naming guidlines doesn't explictly mention variable local to a method, you should name them using
Naming methods, properties and variables in a meaningful way will help you or Mr.Maintainer to faster grasp what a variable/method/class is about.
Shortening of names like
foreach (string s in drwPaths) should be avoided.
If the right side type of an assignment is obvious one should use
var path = "C:\\Users\\SAFEROADS201\\Desktop\\01-VMS Trailer";
By using guard conditions you can save horizontal spacing which improves readability of the code.
By extracting the algorithm to a method in a separate class you can reuse it.
Filepaths should be composed using the
If you are dealing with a large amount of files you should prefer
From the documentation of
The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.
variables should be declared as near as possible to their usage.
strings, especially strings which represents filepaths should be marked as
verbatim string literal to ignore escape sequences. See this SO answer
Let us start by introducing a new class called
SolidWorkFileConverter, add a constructor and add a method named
public class SolidWorkFileConverter
private SldWorks swApplication;
public SolidWorkFileConverter(SldWorks swApp)
swApplication = swApp;
public void ToPdf(string searchPath, string logFileName)
var logFileDestionation = System.IO.Path.Combine(searchPath, logFileName);
var currentDate = DateTime.Now;
var drawingFiles = System.IO.Directory.EnumerateFiles(Path, "*.slddrw", SearchOption.AllDirectories)
.Where(file => !file.Contains("~$"));
foreach (var drawingFile in drawingFiles)
public void ToPdf(string drawingFile)
string destinationFileName = ComposeDestinationFileName(drawingFile);
int state = 0;
int warningState = 0;
ModelDoc2 swDoc = (ModelDoc2)(swApplication.OpenDoc6(s, 3, 0, "", ref state, ref warningState));
state = swDoc.SaveAs3(pdfPath, 0, 0);
swDoc = null;
private string ComposeDestinationFileName(string sourceFileName)
return sourceFileName.Replace("Drawings", "PDFs").Replace("SLDDRW", "PDF");
private void CreateDestinationDirectory(string destinationFileName)
string directoryName = System.IO.Path.GetDirectoryName(destinationFileName);
private void WriteToLog(string logFileDestination, DateTime currentDate)
this can now be called by your
main() method (or used anywhere) like
string searchPath = @"C:\Users\SAFEROADS201\Desktop\01-VMS Trailer";
string logFileName = System.IO.Path.Combine(searchPath , "UpdatedPDFs.txt");
SolidWorkFileConverter converter = new SolidWorkFileConverter(swApp);