C# C从目录中获取所有不带扩展名的文件名
我正在寻找一种方法来读取目录路径中的所有txt文件,而不将其扩展名放入数组中。我查看了path.getFileName,没有outExtension,但它只返回一个文件。我想要指定路径中的所有*.txt文件名 谢谢类似于:C# C从目录中获取所有不带扩展名的文件名,c#,C#,我正在寻找一种方法来读取目录路径中的所有txt文件,而不将其扩展名放入数组中。我查看了path.getFileName,没有outExtension,但它只返回一个文件。我想要指定路径中的所有*.txt文件名 谢谢类似于: String[] fileNamesWithoutExtention = Directory.GetFiles(@"C:\", "*.txt") .Select(fileName => Path.GetFileNameWithoutExtension(fileName
String[] fileNamesWithoutExtention =
Directory.GetFiles(@"C:\", "*.txt")
.Select(fileName => Path.GetFileNameWithoutExtension(fileName))
.ToArray();
我们应该做到这一点
var filenames = Directory.GetFiles(myPath, "*.txt")
.Select(filename => Path.GetFileNameWithoutExtension(filename).Substring(1));
为注释中的规范添加的子字符串1var files=from f in Directory.EnumerateFilesmyPath,*.txt
选择Path.getfilename withoutextensionf.Substring1;
只需将其转换为数组[]
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
}
最后我得到了解决方案。。。我希望它能起作用
另外一个要求是,我需要从所有文件名中删除第一个字符。我该怎么做?一次选择比两次选择更有效你的问题有点不清楚。filename.Substring0,1将只提供第一个字符。filename.Substring1将提供除第一个字符以外的所有内容。
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
}
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;
}
}