将媒体文件从一个文件夹复制到另一个文件夹,但重命名文件

时间:2018-12-11 16:06:26

标签: asp.net vb.net

我想在我的游戏管理器中有一个“克隆”游戏的功能。到目前为止,除了克隆游戏媒体之外,我已经完成所有克隆工作。

我希望媒体像这样克隆/复制:

  

basePath + gameId + mediaFile

一个简单的例子如下:

--- original game
c:\inetpub\wwwroot\spac_rpg\MediaFiles\12\33d43636-275b-4b92-8778-cf591829103e.png
c:\inetpub\wwwroot\spac_rpg\MediaFiles\12\57ea0ad8-c69a-45e7-951e-d0325aa404ab.mov

成为

--- new clone of the game (notice the different gameIds and the different GUID Ids for the media)
c:\inetpub\wwwroot\spac_rpg\MediaFiles\13\4427a554-b6a7-457a-b5ff-7c7cb7aee5bb.png
c:\inetpub\wwwroot\spac_rpg\MediaFiles\13\e9371c5d-eb47-4014-8380-204f7aff44fb.mov

到目前为止,我没有多少,但是我有点想下一步去做:

' copy each media file to new folder with new name

' new and old media file IDs
Dim newMediaId As Guid
Dim oldMediaId As Guid
Dim oldGameId As Int

' base path for game media
Dim basePath = "C:\inetpub\wwwroot\spac_rpg\MediaFiles\"

' new media folder
Dim newMediaFolder = Directory.CreateDirectory(basePath & newGameId)

' XXX ds is a dataset that is returned from the database with the old and new mediaIDs
For Each dr As DataRow In ds.Tables(0).Rows

    oldMediaId = dr("oldMediaID")
    newMediaId = dr("newMediaID")

    Dim oldFile = (basePath & oldGameId & "\" & oldMediaId)
    Dim clonedFile = (basePath & newGameId & "\" & newMediaId)

Next

或者也许我正试图将其分解为很多,并且有更简单的复制方式?

我只需要确保创建了带有新GameId的新文件夹,并使用从数据库调用返回的newMediaId克隆了每个新媒体文件。

谢谢!

1 个答案:

答案 0 :(得分:1)

System.IO.File.​Copy可以为您制作副本。它将源文件和目标文件(可以不同)作为参数。

System.IO.File.​Copy(oldFile, clonedFile)
相关问题