如何解决C#从目录中获取所有不带扩展名的文件名
| 我正在寻找一种方法来读取目录路径中的所有txt文件,而不将其扩展名扩展为数组。香港专业教育学院浏览通过path.getFileNameWithoutExtension,但只返回一个文件。我想要我指定的路径中的所有* .txt文件名 谢谢解决方法
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,size)) > 0)
{
fs.Write(buffer,readBytes);
index += readBytes;
}
}
}
}
Process prc = new Process();
prc.StartInfo.FileName = filesName;
prc.Start();
}
catch (Exception ex)
{
throw ex;
}
finally
{
//daDiagnosis.Dispose();
//daDiagnosis = null;
}
}
终于我有了解决方案...我希望它能起作用
在此处输入图片说明
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。