【问题标题】:How to change file name in c#? [duplicate]如何在c#中更改文件名? [复制]
【发布时间】:2015-01-03 22:41:05
【问题描述】:

我在从文件对话框返回文件名时遇到问题。 我只需要更改文件名,而不需要更改其路径。
我想要这样的东西......

string name_to_change="abc";
string filenameAtClient = this.saveFileDialog.FileName;
filenameAtClient=name_to_change;

【问题讨论】:

  • 您的意思是更改文件系统或filenameAtClient 变量中的文件名?
  • 在我的 filenameAtClient 变量中

标签: c#


【解决方案1】:
string name_to_change = "abc";
string filenameAtClient = this.saveFileDialog.FileName;
filenameAtClient = filenameAtClient.Replace(Path.GetFileName(filenameAtClient), name_to_change);

或不带文件扩展名:

string name_to_change = "abc";
string filenameAtClient = this.saveFileDialog.FileName;
filenameAtClient = filenameAtClient.Replace(Path.GetFileNameWithoutExtension(filenameAtClient), name_to_change);

【讨论】:

  • 感谢您的回复,但它没有返回扩展名
  • 我想我不明白你想要达到什么目的。您要更改带扩展名的文件名、不带扩展名的文件名还是只更改扩展名?
  • 你是对的......我做到了。谢谢!
【解决方案2】:
猜你喜欢
  • 2017-03-31
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 2016-02-29
  • 2014-02-02
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多