C# 将现有文件添加到文件夹

C# 将现有文件添加到文件夹,c#,C#,我是c语言的初学者。在我的项目中,用户通过OpenFileDialog对话框选择图像文件。当他/她选择图像文件时,我正在运行类似以下内容的后台代码: File.Copy(SourceFilePath, DestinationFilePath); 上述代码的问题在于,每当用户试图添加现有图像文件时,它都会抛出错误。为了避免此错误,我将代码更改为以下代码: if (File.Exists(DestinationFilePath)) { intCount++; File.Copy

我是c语言的初学者。在我的项目中,用户通过OpenFileDialog对话框选择图像文件。当他/她选择图像文件时,我正在运行类似以下内容的后台代码:

File.Copy(SourceFilePath, DestinationFilePath);
上述代码的问题在于,每当用户试图添加现有图像文件时,它都会抛出错误。为了避免此错误,我将代码更改为以下代码:

if (File.Exists(DestinationFilePath))
{
     intCount++;
     File.Copy(SourceFilePath,TemporaryFilePath);
     File.Copy(TemporaryFilePath, DestinationFilePath + intCount.ToString());
     File.Delete(TemporaryFilePath);                                
}
else
{
     File.Copy(SourceFilePath, DestinationFilePath);
}
上面代码中的问题是,它在图像文件(如image.gif1)的最末端添加了intCount值,从而更改了文件扩展名。如何将计数器添加到图像文件路径

我认为我在这里使用的检查现有文件的方法是不正确的

更新:答复:-

使用

将int添加到它,并将扩展添加到它,您可以通过

Path.GetExtension(string destinationfilename)
使用

将int添加到它,并将扩展添加到它,您可以通过

Path.GetExtension(string destinationfilename)
而不是:

DestinationFilePath + intCount.ToString()
您可以使用:

intCount.ToString() + DestinationFilePath
这将把它添加到开始,生成1image.gif。

而不是:

DestinationFilePath + intCount.ToString()
您可以使用:

intCount.ToString() + DestinationFilePath

这将把它添加到开始,生成1image.gif。

最好的方法就是创建一个新的文件路径

下面的函数将把计数为1的image.gif设置为image1.gif

private string GetIncrementedFilePath(string orginalFilePath, int count)
{
    var extension = Path.GetExtension(orginalFilePath);
    var fileName = Path.GetFileNameWithoutExtension(orginalFilePath);
    var directory = Path.GetDirectoryName(orginalFilePath);

    var newFullPath = string.Format("{0}\\{1}{2}{3}", directory, fileName, count, extension);

    return newFullPath;
}
请注意,Path.GetExension将为您提供“.gif”而不是“gif”

// Summary:
//     Returns the extension of the specified path string.
//
// Parameters:
//   path:
//     The path string from which to get the extension.
//
// Returns:
//     A System.String containing the extension of the specified path (including
//     the "."), null, or System.String.Empty. If path is null, GetExtension returns
//     null. If path does not have extension information, GetExtension returns Empty.
//
// Exceptions:
//   System.ArgumentException:
//     path contains one or more of the invalid characters defined in System.IO.Path.GetInvalidPathChars().
public static string GetExtension(string path);
如果您想知道某个文件是否存在,那么.NET中内置了一个函数

// Summary:
//     Determines whether the specified file exists.
//
// Parameters:
//   path:
//     The file to check.
//
// Returns:
//     true if the caller has the required permissions and path contains the name
//     of an existing file; otherwise, false. This method also returns false if
//     path is null, an invalid path, or a zero-length string. If the caller does
//     not have sufficient permissions to read the specified file, no exception
//     is thrown and the method returns false regardless of the existence of path.
File.Exists(path);

最好的方法就是创建一个新的文件路径

下面的函数将把计数为1的image.gif设置为image1.gif

private string GetIncrementedFilePath(string orginalFilePath, int count)
{
    var extension = Path.GetExtension(orginalFilePath);
    var fileName = Path.GetFileNameWithoutExtension(orginalFilePath);
    var directory = Path.GetDirectoryName(orginalFilePath);

    var newFullPath = string.Format("{0}\\{1}{2}{3}", directory, fileName, count, extension);

    return newFullPath;
}
请注意,Path.GetExension将为您提供“.gif”而不是“gif”

// Summary:
//     Returns the extension of the specified path string.
//
// Parameters:
//   path:
//     The path string from which to get the extension.
//
// Returns:
//     A System.String containing the extension of the specified path (including
//     the "."), null, or System.String.Empty. If path is null, GetExtension returns
//     null. If path does not have extension information, GetExtension returns Empty.
//
// Exceptions:
//   System.ArgumentException:
//     path contains one or more of the invalid characters defined in System.IO.Path.GetInvalidPathChars().
public static string GetExtension(string path);
如果您想知道某个文件是否存在,那么.NET中内置了一个函数

// Summary:
//     Determines whether the specified file exists.
//
// Parameters:
//   path:
//     The file to check.
//
// Returns:
//     true if the caller has the required permissions and path contains the name
//     of an existing file; otherwise, false. This method also returns false if
//     path is null, an invalid path, or a zero-length string. If the caller does
//     not have sufficient permissions to read the specified file, no exception
//     is thrown and the method returns false regardless of the existence of path.
File.Exists(path);
顺便说一句,请记住,在将文件重命名为类似于file_2.gif的文件后,您仍然可能与目标目录中已有的文件存在名称冲突

string destinationPath;
int index = 0;
do
{
    destinationPath = GetIndexedFilePath(path, ++index);
}
while(File.Exists(destinationPath));
// Copy file to destinationPath
顺便说一句,请记住,在将文件重命名为类似于file_2.gif的文件后,您仍然可能与目标目录中已有的文件存在名称冲突

string destinationPath;
int index = 0;
do
{
    destinationPath = GetIndexedFilePath(path, ++index);
}
while(File.Exists(destinationPath));
// Copy file to destinationPath
您可以使用类路径在包含文件或目录名信息的System.String实例上执行操作,以将表示文件名的字符串与其扩展名分开

范例

string Name = Path.GetFileNameWithoutExtension(DestinationFilePath); //Get the file name excluding its extension
string Extension = Path.GetExtension(DestinationFilePath); //Declare a new string representing the extension of the file
File.Copy(TemporaryFilePath,  DestinationFilePath.Replace(Path.GetFileName(DestinationFilePath), "") + Name + intCount.ToString() + Extension); //Copy from TemporaryFilePath to DestinationFilePath appending a number after the string then the Extension we gathered first
上面的示例将临时文件路径\file_name.Extension的文件名复制到DestinationFilePath\file_name intCount.Extension,其中intCount表示数字,Extension表示文件的扩展名。因此,如果intCount等于1且扩展名为.exe,则文件名的最终外观如下所示

DestinationFilePath\File\u Name 1.exe 谢谢, 我希望这对您有所帮助:

您可以使用类路径在包含文件名或目录名信息的System.String实例上执行操作,以将表示文件名的字符串与其扩展名分开

范例

string Name = Path.GetFileNameWithoutExtension(DestinationFilePath); //Get the file name excluding its extension
string Extension = Path.GetExtension(DestinationFilePath); //Declare a new string representing the extension of the file
File.Copy(TemporaryFilePath,  DestinationFilePath.Replace(Path.GetFileName(DestinationFilePath), "") + Name + intCount.ToString() + Extension); //Copy from TemporaryFilePath to DestinationFilePath appending a number after the string then the Extension we gathered first
上面的示例将临时文件路径\file_name.Extension的文件名复制到DestinationFilePath\file_name intCount.Extension,其中intCount表示数字,Extension表示文件的扩展名。因此,如果intCount等于1且扩展名为.exe,则文件名的最终外观如下所示

DestinationFilePath\File\u Name 1.exe 谢谢,
我希望这对您有所帮助:

您可以使用Path类来操作文件名。您可以使用Path类来操作文件名。它显示错误-进程无法访问文件“filePath”,因为另一个进程正在使用它。到您的第一个post方法。它显示错误-进程无法访问文件“filePath”,因为另一个进程正在使用该文件。感谢您的回复,如果用户再次选择现有文件该怎么办。。我正在检查这需要时间我想这就是为什么你有计数。如果将其增加2,则得到image2.gif。还有一个名为File.Exists的函数,您可以在其中检查文件是否存在,然后删除上一个文件或增加计数。感谢您的响应,如果用户再次选择现有文件该怎么办。。我正在检查这需要时间我想这就是为什么你有计数。如果将其增加2,则得到image2.gif。还有一个名为File.Exists的函数,您可以在其中检查文件是否存在,然后删除上一个或增加计数。