C#从目录中获取所有不带扩展名的文件名
本文关键字:扩展名 文件名 获取 | 更新日期: 2023-09-27 17:58:59
我正在寻找一种方法,在没有扩展名的情况下读取目录路径中的所有txt文件到数组中。我查看了路径.getFileNameWithoutExtension,但它只返回一个文件。我想要来自我指定的路径的所有*.txt文件名
THanks
Directory.GetFiles(myPath, "*.txt")
.Select(Path.GetFileNameWithoutExtension)
.Select(p => p.Substring(1)) //per comment
类似于:
String[] fileNamesWithoutExtention =
Directory.GetFiles(@"C:'", "*.txt")
.Select(fileName => Path.GetFileNameWithoutExtension(fileName))
.ToArray();
应该做这个把戏。
var files = from f in Directory.EnumerateFiles(myPath, "*.txt")
select Path.GetFileNameWithoutExtension(f).Substring(1);
只需要将其转换为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
}
var filenames = Directory.GetFiles(myPath, "*.txt")
.Select(filename => Path.GetFileNameWithoutExtension(filename).Substring(1));
(注释中为规范添加的子字符串(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;
}
}
最后我得到了解决方案。。。我希望它能起作用
在此处输入图像描述