问题描述
请参阅下面的代码段。我上传了文件&保存在物理文件夹中,guid附加到文件名。请看下面。我的目的是在没有guid的情况下取回文件名,如下所示。
Please see below the code snippet. I had uploaded files & saved in physical folder with guid appended to the file name. Please see below. My purpose is to get back the file names without guid as shown below.
HttpPostedFile PostedFile = Request.Files[i];
if (PostedFile.ContentLength > 0)
{
Guid guid = Guid.NewGuid();
string fileNameOrg = Path.GetFileName(PostedFile.FileName);
string extention = Path.GetExtension(fileNameOrg);
string fileNameNew = fileNameOrg.Replace(extention, "") + guid + extention;
PostedFile.SaveAs(path + @"\" + fileNameNew);
}
已保存的文件名:text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt
已保存的文件名:text296a712ff-efd9 -4ab9-9f38-801e0fd23272.txt
现在我的目的是只提取没有GUID的文件名
那是
输出文件名:text1.txt
输出文件名:text2.txt
在此先感谢。
SAVED FILE NAME : text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt
SAVED FILE NAME : text296a712ff-efd9-4ab9-9f38-801e0fd23272.txt
Now My purpose is to extract only the file name without GUID
That is
OUTPUT FILE NAME : text1.txt
OUTPUT FILE NAME : text2.txt
Thanks in advance.
推荐答案
string inp = "text1eb4617fd-4876-4c52-aa5b-21f8be8fb0f8.txt";
const int guidLen = 36;
int extLen = inp.Length - inp.LastIndexOf('.');
string filename = inp.Substring(0, inp.Length - (extLen + guidLen)) + Path.GetExtension(inp);
这篇关于如何从物理位置检索保存的文件名(没有guid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!