xMorok2
xMorok2

Reputation: 3

How to avoid hard coding file paths?

i need to check if a file exists and if the file exsits then the file should be overwritten. It works fine if my hard-coded path is used to my own computer but if it is used on another PC there is an error. How can I check this and overwrite the file?

I have asked google for hours but i have not found a solution.

Dim gamecfg As String = "\Riot Games\League of Legends\Config\game.cfg"

If System.IO.File.Exists(gamecfg) = True Then    
    on.Visible = True
    off.Visible = True
Else
    MsgBox("Game.cfg was not found", MsgBoxStyle.Critical, "Error")
    End
End If

Dim gamecfgSON As String = "\Riot Games\League of Legends\Config\game.cfg"
Dim cfgWriterSON As New System.IO.StreamWriter(gamecfgSON)

cfgWriterSON.Write(RichTextBox1.Text)
cfgWriterSON.Close()

So if the file is located for an example in: E:\Games\League of Legends\ then it says Game.cfg was not found

I hope you can help me.

Upvotes: 0

Views: 237

Answers (3)

Sayali Patil
Sayali Patil

Reputation: 1

try to find out parentpath File dir = new File( System.getProperty("user.dir") + "/.") String parentPath = dir.getParentFile().getName()

Upvotes: 0

tetsuoii
tetsuoii

Reputation: 56

You can usually locate files in one of several ways:

  1. Pull the info from the registry.
  2. Try most obvious combinations of paths.
  3. Heuristic search.
  4. Ask the user.

Heuristic search: Search all folders on all drives for "Riot Games' and 'League of Legends'. Limit search depth and avoid obvious traps like 'Windows*', '$Recycle.Bin', etc.). Usually you'd make several iterations, like first searching only in folders with 'game' in the name.

Upvotes: 0

Christoph
Christoph

Reputation: 3642

You have several possibilities:

  • Check whether the game writes any registry key that contains the path. If that is the case, chances are high that the names of the registry key in the same everywhere.

  • Ask the user in a dialog where the file is, suggesting the default installation path.

  • Let your application be called with a command line parameter that contains the according path.

  • Search the drives, but that is probably gonna be terribly slow.

Upvotes: 1

Related Questions