复制和重命名文件 [英] Copying and renaming file
问题描述
我正在尝试复制和重命名文件
到目前为止,这是我尝试过的事情(显然不起作用):
I''m trying to copy and rename a file
Here is what I''ve tried to do so far(and it''s obviously does''nt work):
.
.
.
Else
Dim diMyDir As New DirectoryInfo(My.Settings.ImgOutput + "/" + IDnumU.Text)
diMyDir.Create()
diMyDir.Attributes = FileAttributes.Hidden
For c As Integer = 0 To lstFiles.Items().Count
System.IO.File.Copy(lstFiles.Items(c).ToString(), diMyDir.FullName.ToString() & "/" & c.ToString())
Next
End If
End Sub
我必须复制文件,然后重命名它,因为此程序正在处理手机的文件.
它找到了我想要的文件路径,但没有将其复制,它显示了运行时错误名称:
NotSupportedException未处理
预先感谢您,Tsahi.
I must copy the file, AND THEN rename it, because this program is handling the Phone''s file.
It found the file path that I want but it does''nt copy it, it displays a Runtime error name:
NotSupportedException was unhandled
Thank you in advance, Tsahi.
推荐答案
您的问题是lstFiles.Items(c)
的类型为ListViewItem
.因此,您将必须使用属性Text
来检索路径.ToString()
将返回包含类型信息(包括实际路径信息)的内容,因此不适合您的用途.
希望这可以帮助您解决问题!
这个小代码示例可能在C#中,但是在将所有文件从D:\Test\Tes
t复制到子目录D:\Test\Test\Test
时,就像将其存储为0、1、2等时一样,它的工作方式很吸引人.
Your problem is thatlstFiles.Items(c)
is of the typeListViewItem
. Thus you''ll have to use the propertyText
to retrieve the path.ToString()
will return something that will contain the type information including the actual path information and thus will not be suitable for your purposes.
I hope this helps you clear the issue!
This small code sample may be in C#, but it works like charm in copying all the files fromD:\Test\Tes
t into the subdirectoryD:\Test\Test\Test
where they are stored as 0, 1, 2, etc. :
static void Main(String[] args)
{
string[] entries = Directory.GetFiles(@"d:\Test\Test\");
for(int c = 0; c < entries.Length; c++)
{
System.IO.File.Copy(entries[c], String.Format(@"D:\Test\Test\Test\{0}", c.ToString()));
}
Console.ReadLine();
}
也许一段工作代码将向您显示您出了问题的地方!
[/编辑]
Maybe a piece of working code will show you where you went wrong!
[/Edit]
Dim dir as new DirectoryInfo(myfoldername);
Dim files as FileInfo() = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
此时,您可以遍历指定文件夹中找到的文件.
At this point, you can iterate through the files found in the specified folder.
foreach file as string in files
' do something with "file"
Next
Tsahi,
根据 MSDN文档 [
您是否已完成调试以确保列出了有效的文件名?如果可能,在确保文件名确实有效之后,是否可以使用错误消息的Visual Studio输出来编辑问题.
霍根
Tsa
According to MSDN documentation[^] for the System.IO.File.Copy method, when you get a NotSupportedException exception, it means "sourceFileName or destFileName is in an invalid format. "
Have you done debugging to ensure that you have valid file names listed? If possible, can you edit your question with the Visual Studio output of the error message after you have ensured that the file names are indeed valid.
Hogan
这篇关于复制和重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!