如何使用通配符使用纯C#.NET重命名命令使用C#重命名文件? [英] How do I rename a file with C# with a pure C# .NET rename command using wildcards?

查看:116
本文介绍了如何使用通配符使用纯C#.NET重命名命令使用C#重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用通配符使用纯C#.NET(无API!)重命名文件,并使用实际的重命名或名称命令,而不是移动或复制或类似的东西。



1)我希望不要移动文件,不建议移动 - 它们是大型大文件!

2)我不知道希望使用Visual Basic命名空间 - 我想用C#来做。

3)我需要在这上面使用通配符!



dos等价物是:ren C:\ServerLogs \ etcc \ * .log * .oldlogs



我尝试过:



虽然VB6甚至VB .NET都有一个用于重命名文件的inbuild命令,但我找不到本机C#命令。只包括VB名称空间,这被排除在外,因为该工具应保持较小并仅使用纯C#运行。

我可以使用dos命令,但是有一个计时问题和路径问题。路径问题是,使用dos命令,只有在logfile目录中打开一个资源管理器窗口时,该工具才有效。如果我改变路径,工具就不能再工作了。所以我尝试使用DOS但是使用currentdir变量会有问题。我在整个.NET上没有发现有关使用C#进行有效重命名的任何内容,只是为了移动整个文件。我不会移动文件来重命名它!

有人更好的想法,即使作为API也不可避免?



注意:请那里没有循环,只需要没有慢速循环的通配符。



我也同意DOS命令。但我需要正确的DOS命令语法,我不依赖curdir,我的应用程序可以在任何目录中工作。

如果可以,请告诉我如何调用DOS以正确的方式命令两个字符串中的完整路径以及如何等到DOS命令真正完成。然后我需要知道它是否真的成功完成。那是DOS的问题,否则我只会用DOS作为C#,因为C#没有强大的命令来处理文件!

How would I rename a File with pure C# .NET (no API!) using wildcards and using actually a real "rename" or "name" command, not a move or copy or something like that.

1) I wish not to move the files, don't suggest to "move" - they are large large files!
2) I don't wish to use the Visual Basic namespace for it - I would like to do it with C#.
3) I need to use Wildcards on this!

the dos equivalent would be: ren C:\ServerLogs\etc\*.log *.oldlogs

What I have tried:

While VB6 and even VB .NET has an inbuild command to rename files I cannot find a native C# command. Only including the VB namespace and this is ruled out, because the tool shall stay small and run only with pure C#.
I was okay to use the dos command, but there is a timing issue and a path issue with it. The path issue is, that with the dos command the tool only works if I have actually an explorer window open in the logfile directory. if I change my path the tool cannot work longer. so I tried to use DOS but it is to problematic with the currentdir variable. I found nothing on the whole .NET about a valid rename with C#, only to MOVE the whole file. I will not move a file to rename it!
Has someone a better idea, if unavoidable even as an API?

Attention: Please no loops there, need only wildcards without slow loops.

I agree on a DOS Command too. But I need the right syntax for the DOS Command that I have not to rely on curdir and my application would work in any directory.
If this is possible, please tell me how I have to call the DOS Command the right way with full paths in both strings and how I can wait until the DOS Command is really finished. And then I need to know if it was really successfully finished. That's the problem with DOS, else I would use only DOS for C#, since C# has no powerful commands to deal with files!

推荐答案

在C#中,你重命名一个文件通过使用 System.IO.File.Move 方法。

如果源文件和目标文件的包含文件夹相同,则文件将不要移动,而是重命名(即,不会有任何文件的副本,只会更改文件名)。



SO:如何在.NET中重命名文件? [ ^ ]

MSDN:File.Move方法(字符串,字符串) [< a href =https://msdn.microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx\"target =_ blank> ^ ]



添加了示例代码

还有一个 Path.ChangeExtension 方法可以满足您的需求。

Path.ChangeExtension方法(字符串,字符串) [ ^ ]

In C#, you rename a file by using the System.IO.File.Move method.
If the containing folder for the source and destination files is the same, the file will not be moved, but renamed instead (i.e., there will not be any copy of the file, only the filename will be changed).

SO: How to rename a file in .NET?[^]
MSDN: File.Move Method (String, String)[^]

Added example code
There is also a Path.ChangeExtension method that could suit your need.
Path.ChangeExtension Method (String, String)[^]
foreach (string filename in Directory.GetFiles(@"C:\ServerLogs\etc", "*.log", SearchOption.TopDirectoryOnly)
{
   Path.ChangeExtension(filename, ".oldlogs");
}


不像DOS那么容易 - 我会从像


$ b开始$ b
Not as easy as DOS unfortunately - I would start with something like

DirectoryInfo d = new DirectoryInfo(@"C:\ServerLogs\etc\");
FileInfo[] infos = d.GetFiles();
foreach(FileInfo f in infos)
{
    String newFileName = // Use A Regex Here To Translate f.FullName -> newFileName 
                         // Or Maybe newFileName = f.FullName.Replace(".log", ".oldlogs");
    File.Move(f.FullName, newFileName);
}


从解决方案2和3,你已经知道你真的需要 File.Move 。我只需要补充一点,即使源文件和目标文件都在不同的目录中,但是在相同的磁盘卷中,它并没有物理移动整个文件 - 文件系统并不是那么愚蠢。它依旧功能更接近重命名。



现在,剩下的就是使用通配符了。没人解释;并且使用正则表达式(解决方案1)的想法是无关紧要的。



实际上,通配符可以带有 System.IO.Directory.GetFiles( string,string,SearchOption),在第二个参数中, searchPattern

Directory.GetFiles方法(String,String,SearchOption)(System.IO) [ ^ ]。



然而,它并不像大多数人合理预期的那样完美。我甚至称它为系统中的缺陷或错误。请看我过去的答案解释: Directory.Get.Files搜索模式问题 [ ^ ]。



-SA
From solution 2 and 3, you already know that you should really need File.Move. I only have to add that even of the source and destination files are in different directories but in the same disk volume, it's not physically moving of the whole file — the file system is not that stupid. It's still closer to renaming, by functionality.

Now, remaining thing is using the wildcard. No one explained it; and the idea of using Regex (Solution 1) is irrelevant.

Really, the wildcards can come with System.IO.Directory.GetFiles(string, string, SearchOption), in a second parameter, searchPattern:
Directory.GetFiles Method (String, String, SearchOption) (System.IO)[^].

However, it works not exactly as most people would reasonably expect. I would even call it a flaw or a bug in the system. Please see my past answer explaining that: Directory.Get.Files search pattern problem[^].

—SA


这篇关于如何使用通配符使用纯C#.NET重命名命令使用C#重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆