File.Move 制作文件副本

发布于 2024-11-27 06:38:38 字数 320 浏览 0 评论 0原文

我有一些代码试图创建一个用作 zip 文件的临时文件。

string tempPath = Path.GetTempFileName(); 
string targetPath = 
    string.Concat(Path.GetTempPath(),"//",Path.GetFileNameWithoutExtension(tempPath),".zip");
File.Move(tempPath, targetPath);

但是,正在创建一个新文件,而不是移动(重命名)临时文件。

我肯定很困惑......

凯文

I have some code where I'm attempting to create a temporary file to be used as a zip file.

string tempPath = Path.GetTempFileName(); 
string targetPath = 
    string.Concat(Path.GetTempPath(),"//",Path.GetFileNameWithoutExtension(tempPath),".zip");
File.Move(tempPath, targetPath);

However a new file is being created instead of moving (renaming) the temp file.

I'm definitely confused...

Kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒七 2024-12-04 06:38:38

我认为这就是您正在寻找的:

FileInfo.MoveTo()

            var newFiles = System.IO.Directory.GetFiles(updateLocation).Select(file => new FileInfo(file));

            var workingDirectory = Environment.CurrentDirectory;
            var existingFiles = System.IO.Directory.GetFiles(workingDirectory).Select(file => new FileInfo(file));

            newFiles.ToList().ForEach(newFile =>
            {
                var matchedFile = existingFiles.ToList().Find(delegate(FileInfo file) { return file.Name == newFile.Name; });

                if(matchedFile != null)
                {
                    if(newFile.LastWriteTimeUtc != matchedFile.LastWriteTimeUtc)
                    {
                        if(!Directory.Exists(TEMP_DIRECTORY_NAME))
                            Directory.CreateDirectory(TEMP_DIRECTORY_NAME);

                        matchedFile.MoveTo(Path.Combine(TEMP_DIRECTORY_NAME, matchedFile.Name));
                        newFile.CopyTo(Path.Combine(workingDirectory, newFile.Name));
                    }
                }
                else
                    newFile.CopyTo(Path.Combine(workingDirectory, newFile.Name));
            });'

I think this is what you're looking for:

FileInfo.MoveTo()

            var newFiles = System.IO.Directory.GetFiles(updateLocation).Select(file => new FileInfo(file));

            var workingDirectory = Environment.CurrentDirectory;
            var existingFiles = System.IO.Directory.GetFiles(workingDirectory).Select(file => new FileInfo(file));

            newFiles.ToList().ForEach(newFile =>
            {
                var matchedFile = existingFiles.ToList().Find(delegate(FileInfo file) { return file.Name == newFile.Name; });

                if(matchedFile != null)
                {
                    if(newFile.LastWriteTimeUtc != matchedFile.LastWriteTimeUtc)
                    {
                        if(!Directory.Exists(TEMP_DIRECTORY_NAME))
                            Directory.CreateDirectory(TEMP_DIRECTORY_NAME);

                        matchedFile.MoveTo(Path.Combine(TEMP_DIRECTORY_NAME, matchedFile.Name));
                        newFile.CopyTo(Path.Combine(workingDirectory, newFile.Name));
                    }
                }
                else
                    newFile.CopyTo(Path.Combine(workingDirectory, newFile.Name));
            });'
乖乖兔^ω^ 2024-12-04 06:38:38

这是预期的行为。如果您想删除旧文件,则需要明确执行此操作。

That's expected behavior. If you want the old file deleted, you need to explicitly do that.

谁对谁错谁最难过 2024-12-04 06:38:38

你的代码对我有用。

Path.GetTempFileName()

将在您计算机的 %TEMP% 目录中创建一个新的 0 字节临时文件。运行 File.Move 行后,临时文件将使用 .zip 扩展名重命名。

之后您现在可以使用该文件:

using (var writeStream = new FileStream(targetPath, FileMode.Open, FileAccess.ReadWrite))
{
    // CODE HERE

}

Your code works for me.

Path.GetTempFileName()

will create a new 0 byte temporary file in your machines %TEMP% directory. After the File.Move line is run the temp file is renamed with a .zip extension.

After this you can now use the file:

using (var writeStream = new FileStream(targetPath, FileMode.Open, FileAccess.ReadWrite))
{
    // CODE HERE

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文