hWorld
hWorld

Reputation: 185

C# Get All file names without extension from directory

Im looking for a way to read ALL txt files in a directory path without their extensions into an array. Ive looked through the path.getFileNameWithoutExtension but that only returns one file. I want all the *.txt file names from a path i specify

THanks

Upvotes: 6

Views: 20591

Answers (6)

rakesh
rakesh

Reputation: 1

public void getTestReportDocument(string reportid, string extenstype)
{
    try
    {
        string filesName = "";
        if (sqlConn.State == ConnectionState.Closed)
            sqlConn.Open();
        if(extenstype == ".pdf")
        {
            filesName = Path.GetTempFileName();
        }
        else
        {
            filesName = Path.GetTempFileName() + extenstype;
        }

        SqlCommand cmd = new SqlCommand("GetTestReportDocuments", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ReportID", reportid);

        using (SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.Default))
        {
             while (dr.Read())
             {
                 int size = 1024 * 1024;
                 byte[] buffer = new byte[size];
                 int readBytes = 0;
                 int index = 0;
                 using (FileStream fs = new FileStream(filesName, FileMode.Create, FileAccess.Write, FileShare.None))
                 {
                     while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
                     {
                         fs.Write(buffer, 0, readBytes);
                         index += readBytes;
                     }
                 }
             }
        }
        Process prc = new Process();
        prc.StartInfo.FileName = filesName;
        prc.Start();
   }

   catch (Exception ex)
   {
       throw ex;
   }
   finally
   {
       //daDiagnosis.Dispose();
       //daDiagnosis = null;
   }
}

finally i got solution... i hope it will work
enter image description here

Upvotes: -1

Sara N
Sara N

Reputation: 1189

just need to convert it to Array[]

   string targetDirectory = @"C:\...";

// Process the list of files found in the directory. 
   string[] fileEntries = Directory.GetFiles(targetDirectory,  "*.csv").Select(Path.GetFileNameWithoutExtension).Select(p => p.Substring(0)).ToArray();

   foreach (string fileName in fileEntries)
        {
          //Code
        }

Upvotes: 1

aligray
aligray

Reputation: 2832

var files = from f in Directory.EnumerateFiles(myPath, "*.txt")
            select Path.GetFileNameWithoutExtension(f).Substring(1);

Upvotes: 1

Falanwe
Falanwe

Reputation: 4744

var filenames = Directory.GetFiles(myPath, "*.txt")
.Select(filename => Path.GetFileNameWithoutExtension(filename).Substring(1));

(the substring(1)) added for a specification in commentary)

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68667

Directory.GetFiles(myPath, "*.txt")
    .Select(Path.GetFileNameWithoutExtension)
    .Select(p => p.Substring(1)) //per comment

Upvotes: 17

DaveShaw
DaveShaw

Reputation: 52788

Something like:

String[] fileNamesWithoutExtention = 
Directory.GetFiles(@"C:\", "*.txt")
.Select(fileName => Path.GetFileNameWithoutExtension(fileName))
.ToArray();

Should do the trick.

Upvotes: 5

Related Questions