vb.net 将资源复制到磁盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16816920/
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 13:47:11  来源:igfitidea点击:

Copy Resources to disk

vb.netvisual-studio-2012

提问by S Chase

First attempt at a VB.NET application. I am trying to create an application to copy standard configurations for an application then customize certain settings for that user. I have most of the application working fine but I'm having an issue with resource files.

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

Because the number of files may change I'm using a sub-folder for my resources. I have the files set to content and always copy. I can get the following code to work in debug but it seems I'm not doing it properly for build.

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

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

I have attempted several ways of copying the data but I can find nothing compatible, like the following.

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

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

Can someone point me in the proper and correct direction to do this? Currently the file types are 1 .vbs and 1 .exe.config file but I may need to add different file types in the future.

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

采纳答案by matzone

About this code ..

关于这个代码..

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

If you access some special folder such as Desktop and retrieve from app resource , you make it like this

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

File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & strFile, My.Resources.ResourceManager.GetObject(strFileName), True)

回答by maxedev

It seems the issue is this line For Each strFile In Directory.GetFiles(".\Files")

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

.\Files is referring to a relative path that doesn't exist when you move the .exe.

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