- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有时,在我的应用程序中,我想创建一个已经存在的文件的副本,但我不想测试该文件是否已经存在,我只想创建该文件的副本,比如Windows 7 可以。
例如:文件tips.txt。当我的应用复制它时,将创建另一个名为 tips - copy.txt 的文件。之后,如果有必要,一个“copy of a copy”tips - copy - copy.txt。
在这种情况下我能做些什么吗?
观察:在此应用程序中,我使用的是 .NET 3.5 和 WPF。
Obs2:我提出这个问题是因为我认为.NET 中已经存在类似的东西。
最佳答案
您应该提取文件名和扩展名,然后使用新的格式化名称执行简单的 File.Copy
string fileName = "tips.txt";
string file = Path.GetFileNameWithoutExtension(fileName);
string ext = Path.GetExtension(fileName);
File.Copy(fileName, string.Format("{0} - Copy{1}", file, ext);
如果你有完整的复制路径,事情会变得有点复杂
string fileName = "C:\\test\\tips.txt";
string path = Path.GetDirectoryName(fileName);
string file = Path.GetFileNameWithoutExtension(fileName);
string ext = Path.GetExtension(fileName);
File.Copy(fileName, Path.Combine(path, string.Format("{0} - Copy{1}", file, ext));
但如果您真的想模仿 Windows 资源管理器的行为,我们应该这样做:
string fileName = "C:\\test\\tips.txt";
string path = Path.GetDirectoryName(fileName);
string file = Path.GetFileNameWithoutExtension(fileName);
string ext = Path.GetExtension(fileName);
if(file.EndsWith(" - Copy")) file = file.Remove(0, file.Length - 7);
string destFile = Path.Combine(path, string.Format("{0} - Copy{1}", file, ext));
int num = 2;
while(File.Exists(destFile))
{
destFile = Path.Combine(path, string.Format("{0} - Copy ({1}){2}", file, num, ext));
num++;
}
File.Copy(fileName, destFile);
如果 Windows Explorer 复制以“-Copy”结尾的文件,它会向目标文件添加一个递增编号,而不是另一个“-Copy”。
您还应该考虑到字符串“Copy”已本地化,因此它会在非英语版本的操作系统中发生变化。
关于c# - 像 Windows7 通常那样创建一个文件的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12708372/
时间过后如何用javascript或任何其他解释语言实现的函数被调用? 换句话说,在javascript解释器中是否存在循环,该循环始终检查计时器列表,或者操作系统是否有任何特定方式来处理此循环? 谢
我有项目要做。假设是这样的: 从带有单词的文件中,我只需要阅读字母单词(进入名为Words的列表-下面的代码)-我是使用lib中的isalpha()函数做到的。列表的每个元素包含一个单词 我需要从输入
我有两套test和 test1我需要从 test 中删除元素存在于 test1例如 如果test包含 1,2,3,4,5 和 test1包含 3,5,6,7:那么函数应该在 test 上执行所以里面只
引用指针导致间接使用对象的值。但我从来没有真正理解“使用”是什么意思。我开始思考这个问题,直到我的编译器对以下代码产生错误 int i = 0, *pi = &i; decltype(*pi) c;
我读了很多关于 Taint在 Perl 变量、模式等中,例如来自 docs : $AUTOLOAD can now be tainted If you call a subroutine by a t
我一直在研究 FP 语言(断断续续),并且使用过 Scala、Haskell、F# 和其他一些语言。我喜欢我所看到的并理解 FP 的一些基本概念(绝对没有范畴论的背景——所以请不要谈论数学)。 所以,
在C语言中,当您想要进行通用编程时,唯一受语言支持的选项是宏。它们效果很好并且被广泛使用,但是如果可以使用内联函数或常规函数,则不建议使用它们。 (如果使用gcc,您还可以使用gcc statemen
我有一些代码可以进行 requests.get() 调用,这可能会以各种方式失败。我想捕获 requests 异常,但不关心调用失败的原因。 我想避免这样的代码 try: r = reques
我正在尝试编写一个 C 解析器,用于我自己的教育。我知道我可以使用像 YACC 这样的工具来简化这个过程,但我想尽可能多地从经验中学习,所以我从头开始。 我的问题是我应该如何处理这样的一行: doSo
我正在设计一个系统,但我认为让最终用户能够删除数据库中的条目并不是一个好主意。我是这么认为的,因为通常最终用户一旦获得管理员权限,最终可能会在数据库中弄得一团糟,然后求助于我来修复它。 当然,如果他们
我们有两张 table 。第一个包含名称 (varchar) 字段。第二个包含引用第一个表中的名称字段的字段。第二个表中的外键将针对与该名称关联的每一行重复。通常不鼓励使用 varchar/strin
我正在为 Apple Watch 开发一个非常简单的锻炼应用程序。它使用 Health Kit 开始和结束锻炼,我唯一的问题是,当我尝试结束锻炼时,它通常不会结束 session ,我收到此错误 20
当前,我们在执行sql查询脚本时遇到一些失败的问题。 错误讯息为, ” 系统断言检查失败。检查SQL Server错误日志以获取详细信息。通常,断言失败是由软件错误或数据损坏引起的。要检查数据库是否损
这是一个非常简单的问题,但我似乎找不到在某些情况下选择一个而不是另一个的一般规则。 假设我有一个简单的 Point 类,如下所示: class Point { public: Point();
这个问题在这里已经有了答案: Java Programming - Where should SQL statements be stored? [closed] (15 个答案) 关闭 9 年前。
我很欣赏这个问题可能有点“基于意见”,但是,我认为一个概括的答案可以极大地帮助将来有兴趣了解更多关于 PWA 和 Service Workers 的其他人。 背景 我在网上搜索过,但很惊讶地发现没有明
对于编程语言解释器,我想知道解释器经历的事件顺序。例如,我认为事情是这样的: 解释器得到一些输入 词法分析器/分词器获取输入并划分标记 x 获取代币列表 ??? 代码被执行 什么步骤属于 ??? sp
我是 Flutter 的新手,刚刚从阅读有关 Flutter 的教程中听说了 BLoC 概念。从这里 tutorial ,第一次听说BLoC。但是我在这篇文章中也看到了一个名为“Repository”
我正在为下/上三角矩阵(double)编写一个类。通过利用 n*n 三角矩阵只有 n*(n + 1)/2 [可能非零] 元素这一事实,我在内部只存储平面数组成员中的元素数量。 首先,我有一个“普通”(
我有一个 Java src 文件夹,我在其中存储我的 .java 文件。然后我使用终端编译它们并最终在同一目录中获得 .class 文件。这不一定会打扰我,但我从未见过专业人士这样做。 按照专业惯例(
我是一名优秀的程序员,十分优秀!