应用程序数据路径在开发与部署之间 [英] app data path during development versus deployment

查看:168
本文介绍了应用程序数据路径在开发与部署之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我打算部署了Windows XP的Windows窗体.Net应用程序,该应用程序通过将所有文件复制到应用程序文件夹中部署到更新的Windows操作系统使用InstallShield或高级安装程序,并将dll放置在应用程序安装文件夹,内容文件在ProgramData和/或用户的AppData。



所以在Visual Studio 2010(可能只是将它们保留在现在的位置,应用程序的bin / debug文件夹中)以及部署时的其他位置时,内容文件将位于一个位置。



如果我有一个包含基数的全局字符串,那么在Visual Studio调试期间,如何使用这些文件,路径的内容文件,那么我可以使用相对于该字符串的路径访问这些文件。但是我不确定如何在调试期间创建一个具有正确路径的字符串,然后在部署期间创建一个不同的路径。



我知道我可以测试Debug vs Release标志,但是那是不一样的事情。 (切换到发布版本只是把文件移动到../bin/Release而不是../bin/Debug;可能还没有被部署。)



有没有要简单的说明一下如何实现这一点?



要清楚的是,我不是在询问访问相对于基本目录的路径的细节。我正在问如何区分运行部署,并在开发过程中运行。



所以知道如何检测我被部署是我需要的最小帮助。更好的将是一个小型示例或链接到教程,显示在开发期间访问一个位置的内容文件,以及部署时的不同位置。






更新



在c#中最好的方法是确定程序员是通过IDE运行程序还是用户?
涵盖了最重要的情况,所以如果没有其他解决方案,我会使用它。但是,如果开发人员直接双击开发项目的bin / debug文件夹中的.exe,它将无法正常工作。因为它们不在IDE中运行(我们也不使用vshost.exe),而是与基本文件夹相同。



更新



进一步反思,上面提出的stackoverflow Q& A并不完全相同。我不在乎调试器是否附加(可以将调试器附加到已安装/部署的版本,而且仍然是部署版本,而不是开发版本)。



我最初认为可能有一些标准的标志或配置设置,该应用可以用来确定它已经安装。



人们如何知道哪里要查找他们的内容文件,因为在开发和安装过程中它们不会在同一个地方? (除非你在应用安装文件夹中放置内容文件的旧学校方法,我以前是什么,但现在需要做的不同)。






更新



最后我突然显示,我不应该试图保留内容文件在开发过程中的bin / debug文件夹 - 只是这样做,因为这是以前的。还没有决定是否将它们全部移动到他们将被部署的位置,或者在开发机器上的其他位置。



我还好奇其他人在开发过程中指定内容文件的位置,但也许这是一个不同的问题...

解决方案

在两个可能的位置查找文件。然后缓存目录路径以便将来进行内容访问。在我的情况下,开发位置是bin文件夹旁边的内容文件夹。

 语言:Visual Basic(VB)
'...这是一个VB模块...
Public g_AppRelativePath As String =CompanyName\AppName
Private _contentPathRootWithSlash As String

公共函数ContentPath(relPath As String)As String
如果_contentPathRootWithSlash不是,然后
'---首先尝试查找开发内容---
'包含。可执行程序。
Dim pathRoot As String = Application.StartupPath
Dim binIndex As Integer = pathRoot.LastIndexOf(bin,StringComparison.Ordinal)
如果binIndex> = 0然后
'内容文件夹,它被准备为bin的兄弟。
pathRoot = pathRoot.Remove(binIndex)& content
End If
Dim pathRootWithSlash As String = pathRoot& \

如果File.Exists(pathRootWithSlash& relPath)然后
_contentPathRootWithSlash = pathRootWithSlash

Else
'---开发内容不存在;寻找部署的内容。 ---
''使用这个,如果想要的文件在用户的AppData \Roaming。
'pathRootWithSlash = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)& \
'使用这个,如果想要所有用户在ProgramData中共享文件。
pathRootWithSlash = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)& \
'g_AppRelativePath以前设置为CompanyName\AppName,它与我们的安装程序中的逻辑相匹配。
pathRootWithSlash = pathRootWithSlash& g_AppRelativePath& \
如果File.Exists(pathRootWithSlash& relPath)然后
_contentPathRootWithSlash = pathRootWithSlash

Else
'无法找到。
MsgBox(String.Format(找不到内容文件{0},relPath))
返回没有
结束如果
结束如果
结束如果

返回_contentPathRootWithSlash& relPath
结束函数


I have an old Windows Forms .Net application that on Windows XP was "deployed" by copying all the files to the application folder.

Now I plan to deploy to newer windows OS's using either InstallShield or Advanced Installer, and will be placing the dlls in app install folder, and the content files in ProgramData and/or user's AppData.

So the content files will be in one location during debugging under Visual Studio 2010 (probably just keep them where they are now, in app's bin/debug folder), and another location when deployed.

How do I make access to these files work the same way, during Visual Studio debugging, as during deployment?

If I had a global string that contains the base path for the content files, then I could access the files with a path relative to that string. But I am unsure how to create a string that has correct path during debugging, and then a different path during deployment.

I know I can test Debug vs Release flags, but that isn't quite the same thing. (switching to release build just moves the files to ../bin/Release instead of ../bin/Debug; might still not be deployed.)

Is there a simple example of how to accomplish this?

To be clear, I'm not asking about the details of accessing a path relative to a base directory. I'm asking how to distinguish between running deployed, and running during development.

So knowing how to detect "I am deployed" is the minimum help I need. Even better would be a mini-example or link to tutorial that shows accessing content files in one location during development, and a different location when deployed.


UPDATE

What is the best way in c# to determine whether the programmer is running the program via IDE or it's user? covers the most important case, so I would use it if there were no other solution. However, it does not work correctly if a developer double-clicks directly on the .exe within the development project's bin/debug folder. Because they aren't running within IDE (nor are we using vshost.exe), but the base folder is the same as if they were.

UPDATE

Upon further reflection, that stackoverflow Q&A suggested above is not at all the same thing. I don't care whether a debugger is attached (one could attach a debugger to the installed/deployed version, and it would still be a deployed version, not a development version).

I was originally thinking that there might be some standard flag or config setting somewhere that the app could use to determine that it is installed.

How do people know where to look for their content files, given that they won't be in the same place during development versus an installation? (Unless you do the "old school" approach of putting content files in your app install folder. Which is what I had before, but now need to do differently.)


UPDATE

Finally had the epiphany that I shouldn't be trying to keep content files in the bin/debug folder during development - was only doing that because that's how it was before. Haven't decided whether to move them all to the location they will be once they are deployed, or to some other location on the development machines.

I am still curious how other people specify location of content files during development, but perhaps that is a different question...

解决方案

I solved this by explicitly looking for a file in two possible locations. Then caching the directory path for future content access. In my case, the "development" location is a "content" folder that is next to the "bin" folder.

' Language: Visual Basic (VB)
' ... this is inside a VB module ...
Public g_AppRelativePath As String = "CompanyName\AppName"
Private _contentPathRootWithSlash As String

Public Function ContentPath(relPath As String) As String
  If _contentPathRootWithSlash Is Nothing Then
    ' --- first try to find "development" content ---
    ' Folder containing .exe.
    Dim pathRoot As String = Application.StartupPath
    Dim binIndex As Integer = pathRoot.LastIndexOf("bin", StringComparison.Ordinal)
    If binIndex >= 0 Then
      ' "content" folder that has been prepared as a sibling of "bin".
      pathRoot = pathRoot.Remove(binIndex) & "content"
    End If
    Dim pathRootWithSlash As String = pathRoot & "\"

    If File.Exists(pathRootWithSlash & relPath) Then
      _contentPathRootWithSlash = pathRootWithSlash

    Else
      ' --- "development" content does not exist; look for "deployed" content. ---
      '' Use this, if want files to be in User's AppData\Roaming.
      'pathRootWithSlash = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\"
      ' Use this, if want files to be shared by all users, in ProgramData.
      pathRootWithSlash = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\"
      ' g_AppRelativePath was previously set to "CompanyName\AppName", which matches logic in our installer.
      pathRootWithSlash = pathRootWithSlash & g_AppRelativePath & "\"
      If File.Exists(pathRootWithSlash & relPath) Then
        _contentPathRootWithSlash = pathRootWithSlash

      Else
        ' Failed to find.
        MsgBox(String.Format("Can't find content file ""{0}""", relPath))
        Return Nothing
      End If
    End If
  End If

  Return _contentPathRootWithSlash & relPath
End Function

这篇关于应用程序数据路径在开发与部署之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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