Windows環境およびいくつかのアプリケーションでは、既存のファイルと同じ名前で新しいファイルを作成すると、出力ファイルが既に存在するというメッセージは表示されませんが、(1)で新しいファイルが作成されます。ファイル名の終わり...
name.doc
になります
name(1).doc
C#で同じ動作を作成しようとしていますが、質問は...このコードをすべて削減できますか?
FileInfo finfo = new FileInfo(fullOutputPath);
if (finfo.Exists)
{
int iFile = 0;
bool exitCreatingFile = false;
while (!exitCreatingFile)
{
iFile++;
if (fullOutputPath.Contains("(" + (iFile - 1) + ")."))
fullOutputPath = fullOutputPath.Replace(
"(" + (iFile - 1) + ").",
"(" + iFile + ")."); // (1) --> (2)
else
fullOutputPath = fullOutputPath.Replace(
Path.GetFileNameWithoutExtension(finfo.Name),
Path.GetFileNameWithoutExtension(finfo.Name) + "(" + iFile + ")"); // name.doc --> name(1).doc
finfo = new FileInfo(fullOutputPath);
if (!finfo.Exists)
exitCreatingFile = true;
}
}