vb.net 更改现有文件的扩展名:Visual Studio 2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14759508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:14:43  来源:igfitidea点击:

Changing an existing file's extension: Visual Studio 2010

vb.netvisual-studio-2010

提问by Samuel Pardee

After searching for quite sometime now, one cannot just find a simple answer. So here is my question:

经过一段时间的搜索,人们无法找到一个简单的答案。所以这是我的问题:

I am writing in Visual Basic from Visual Studio 2010.
(Another question that I need answered but is not relevant: Is this language referred to as VB .NET ?; I am new to this language but not to programming as I am a web designer.)

我正在使用 Visual Studio 2010 的 Visual Basic 编写。
(另一个我需要回答但不相关的问题:这种语言是否称为 VB .NET ?;我是这种语言的新手,但不熟悉编程,因为我是一名网页设计师.)

I have a file in this directory: C:\users\Debbie
This file's name and extension are as followed: backup01.zip

我在这个目录下有一个文件:C:\users\Debbie
这个文件的名字和扩展名如下:backup01.zip

What I need to know is:How do I take this file that I have created and quickly change the extension without moving it to another directory or making a copy. I just want my program to find the file backup01.zip and quickly change it to backup01.nbu

我需要知道的是:如何获取我创建的这个文件并快速更改扩展名,而无需将其移动到另一个目录或进行复制。我只是想让我的程序找到文件 backup01.zip 并快速将其更改为 backup01.nbu

.nbu will be the file extensions that my software will be able to open (when I get there).

.nbu 将是我的软件能够打开的文件扩展名(当我到达那里时)。

Thanks..

谢谢..

(P.S.) If anyone is curious as to why I am already writing files it would be because I am jumping into this and trying my best to create simple backup software for my Mother whose original software that she uses frequently on her computer does not have backup capabilities. Keep in mind that I am an aspired programmer familiar with languages such as HTML, CSS, PHP etc. and I am trying to pick this one up by myself as well.

(PS) 如果有人对我为什么已经在写文件感到好奇,那将是因为我正在跳入这个领域并尽我所能为我母亲创建简单的备份软件,她在她的计算机上经常使用的原始软件没有备份能力。请记住,我是一名有抱负的程序员,熟悉 HTML、CSS、PHP 等语言,我也在尝试自己选择这个。

回答by driis

Use the File.Movemethod:

使用File.Move方法:

System.IO.File.Move("C:\users\Debbie\backup01.zip", "C:\users\Debbie\backup01.nbu")

You likely have the file name in a variable already, so Path.ChangeExtensionwill be handy:

您可能已经在变量中拥有文件名,因此Path.ChangeExtension会很方便:

Dim originalFile As String = "C:\users\Debbie\backup01.zip"
Dim newName As String = Path.ChangeExtension(originalFile, "nbu")
File.Move(originalFile,newName)

回答by Sreenikethan I


Seems like even I'm using Visual Basic 2010!
I would recommend you to use this:


似乎连我都在使用Visual Basic 2010
我建议你使用这个:

My.Computer.FileSystem.RenameFile("C:\users\Debbie\backup01.zip", "C:\users\Debbie\backup01.nbu")

That's all. Hope it helps!
Cheers, Sreenikethan

就这样。希望能帮助到你!
干杯,斯里尼克坦

回答by Denis

First the simple question - yes, it is called vb.net (2002 it became a .NET Framework language, before that the last version was called VB 6.0)

首先是一个简单的问题 - 是的,它被称为 vb.net(2002 年它成为一种 .NET 框架语言,在此之前最后一个版本被称为 VB 6.0)

As to the renaming issue, here is the code:

至于重命名问题,这里是代码:

Dim goodFileName As String = "C:\mydir\myfile.com.extension"        
Dim result As String= Path.ChangeExtension(goodFileName, ".old")
File.Move(goodFileName ,result)

The ChangeExtensiononly returns the new full path, but does not edit the file itself in any way.

ChangeExtension只返回新的完整路径,但以任何方式不编辑文件本身。

回答by WildOne

I have just recently made my first program in Visual Studio 2013 and I have no technical programming skills, so bear over with me here. What you want to achieve is easy in command line and as far as I know this should work in Visual Studio:

我最近刚刚在 Visual Studio 2013 中制作了我的第一个程序,但我没有技术编程技能,所以在这里耐心等待。您想要在命令行中实现的目标很容易,据我所知,这应该在 Visual Studio 中工作:

Process.Start("cmd.exe", "/c ren C:\users\Debbie\backup01.zip backup01.nbu")

It starts the command line /c ends it when it's done and the rest is self explanatory. It is a little "workaround", but it will get the job done.

它启动命令行 /c 完成后结束它,其余的不言自明。这是一个小小的“解决方法”,但它会完成工作。