Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# select excel file from directory path

C# select excel file from directory path

Scheduled Pinned Locked Moved C#
questioncsharphelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    classy_dog
    wrote on last edited by
    #1

    In a C# 2010 desktop application, I want to change the application so that it will only allow users to select a report that meets the following criteria: 1. part of the file name is "ErrorReport" and 2. The last node of the file name is .xlsx or .xls. So far I have the following code:

    string[] excelFiles = Directory.GetFiles(strDirectoryLoc, "*ErrorReport*")
    .Select(path => Path.GetFileName(path))
    .Where(x => (x.EndsWith(".xlsx") || x.EndsWith(".xls"))
    && (!x.StartsWith("~")))
    .ToArray();

    This code works when I am selecting only excel (*.xls or *.xlsx) files. The problem occurs if the user selects a .pdf file iniitally and there is actually a file in the directory path that meets the criteria I listed above. The code will ignore the .pdf file the user selects and will actually use the excel file that is in the directory path. Thus how can I change the code listed above to say the .pdf file is invalid?

    R 1 Reply Last reply
    0
    • C classy_dog

      In a C# 2010 desktop application, I want to change the application so that it will only allow users to select a report that meets the following criteria: 1. part of the file name is "ErrorReport" and 2. The last node of the file name is .xlsx or .xls. So far I have the following code:

      string[] excelFiles = Directory.GetFiles(strDirectoryLoc, "*ErrorReport*")
      .Select(path => Path.GetFileName(path))
      .Where(x => (x.EndsWith(".xlsx") || x.EndsWith(".xls"))
      && (!x.StartsWith("~")))
      .ToArray();

      This code works when I am selecting only excel (*.xls or *.xlsx) files. The problem occurs if the user selects a .pdf file iniitally and there is actually a file in the directory path that meets the criteria I listed above. The code will ignore the .pdf file the user selects and will actually use the excel file that is in the directory path. Thus how can I change the code listed above to say the .pdf file is invalid?

      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #2

      I think you are looking for something like:

      string[] excelFiles = Directory.GetFiles(strDirectoryLoc, "*ErrorReport*")
      .Select(path => Path.GetFileName(path))
      .Where(x => (x.EndsWith(".xlsx") || x.EndsWith(".xls"))
      && (!x.StartsWith("~") && x.Contains("ErrorReport")))
      .ToArray();

      You can also use Path.GetFilenameWithoutExtension and check that the string has ErrorReport in it.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups