1

第一次尝试 VB.NET 应用程序。我正在尝试创建一个应用程序来复制应用程序的标准配置,然后为该用户自定义某些设置。我的大部分应用程序工作正常,但资源文件有问题。

因为文件的数量可能会发生变化,所以我正在为我的资源使用一个子文件夹。我将文件设置为内​​容并始终复制。我可以让以下代码在调试中工作,但似乎我没有正确地进行构建。

For Each strFile In Directory.GetFiles(".\Files")

        If Path.GetExtension(Path.GetFileName(strFile)) = ".vbs" Then
            strDestination = strDataPath & "Scripts\"
        Else
            strDestination = strDataPath
        End If
        If Not Directory.Exists(strDestination) Then
            Directory.CreateDirectory(strDestination)
        End If

        If My.Settings.chkForceOverwrite = True Then
            Try
                File.Copy(strFile, strDestination & Path.GetFileName(strFile), True)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        Else
            Try
                File.Copy(strFile, strDestination & Path.GetFileName(strFile), False)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End If
    Next

我尝试了几种复制数据的方法,但找不到任何兼容的方法,如下所示。

Dim strFileName As String = Path.GetFileNameWithoutExtension(strFile)
File.WriteAllBytes(strDestination & strFile, My.Resources.strFileName, True)

有人可以指出正确和正确的方向吗?目前文件类型是 1 个 .vbs 和 1 个 .exe.config 文件,但我以后可能需要添加不同的文件类型。

4

2 回答 2

1

关于这段代码..

File.WriteAllBytes(strDestination & strFile, My.Resources.strFileName, True)

如果您访问某些特殊文件夹(例如 Desktop 并从应用程序资源中检索),则可以这样

File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & strFile, My.Resources.ResourceManager.GetObject(strFileName), True)
于 2013-05-29T15:15:36.167 回答
0

看来问题是这条线For Each strFile In Directory.GetFiles(".\Files")

.\Files 是指移动 .exe 时不存在的相对路径。

于 2013-05-29T15:07:47.540 回答