Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Asp.net替换文件名(特定文件夹)?_Asp.net_C# 4.0 - Fatal编程技术网

如何使用Asp.net替换文件名(特定文件夹)?

如何使用Asp.net替换文件名(特定文件夹)?,asp.net,c#-4.0,Asp.net,C# 4.0,我可以替换特定文件夹中的文件名,我是这样写的 FileInfo fsource = new FileInfo(Server.MapPath("~/PurchaseOrder/" + lblhideid.Text)); if (fsource.Exists) { string[] file = lblhideid.Text.Split('.');

我可以替换特定文件夹中的文件名,我是这样写的

FileInfo fsource = new FileInfo(Server.MapPath("~/PurchaseOrder/" + lblhideid.Text));
                        if (fsource.Exists)
                        {
                            string[] file = lblhideid.Text.Split('.');
                            string fName="Z-"+System.DateTime.Now.ToString("MM-dd-yyyy")+"-"+saveConsultantID+"."+file[1];
                            fsource.Name.Replace(lblhideid.Text, fName);

                        }
lblhideid.Text=image.jpeg,所以我可以替换我自己的名字,比如fName,如何替换这个名字请给我任何建议

谢谢你
Hemanth

试试这个,如果他们放一个像file.tar.gz这样的文件名怎么办

string extension = Path.GetExtension("~/PurchaseOrder/" + lblhideid.Text);
string newName = "MYFILE." + extension

File.Move(
    "~/PurchaseOrder/" + lblhideid.Text,
    "~/PurchaseOrder/" + newName );

我猜你希望最后一行是:

fsource.MoveTo(Server.MapPath("~/PuchaseOrder/" + fName));
您当前的代码仅获取作为字符串的文件名并操纵该字符串。您希望操纵文件本身

编辑:

您确定存在
~/PurchaseOrder/

尝试:


找不到路径的一部分。是错误吗?请给我另一个建议。您确定~/PurchaseOrder/存在吗?你能一步一步地看代码a吗?看哪一部分找不到路径?
string originalPath = Server.MapPath("~/PurchaseOrder/" + lblhideid.Text);

FileInfo fsource = new FileInfo(originalPath);
if (fsource.Exists)
{
     string newName = string.Format("Z-{0:MM-dd-yyyy}-{1}.{2}",
                                    System.DateTime.Now,
                                    saveConsultantID,
                                    fsource.Extension);

     string newPath = Path.Combine(fsource.DirectoryName, newName);
     fsource.MoveTo(newPath);                   
}