1

I am struggling with how to make my app work when deployed - it keeps saying the file is invalid no matter what I do: What I have now is:

Data Source="\MyDb.sdf"

The DB should be in the directory where the executable is located.

2
  • 1
    That is a relative path, does MyDb.sdf exist in the application directory? Commented Jan 31, 2014 at 15:36
  • Yes, it does and I would like to have it this way. Commented Jan 31, 2014 at 15:39

1 Answer 1

2

You probably need to use an absolute path to access the file.

var pathToExe = System.Reflection.Assembly.GetExecutingAssembly().Location;
var path = Path.GetDirectoryName(pathToExe);
var pathToDb = Path.Combine(path, "MyDb.sdf");

pathToDb should now be an absolute path to your database object, assuming it is always in the directory of the executing assembly.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm in the exception, it shows the correct path but still says that the path is invalid.
I hate to ask again, but you're absolutely sure that the file MyDb.sdf explicitly exists in the path?
Oh, actually your code returns the folder AND the exe file so the output us XX.exe\\MyDb.sdf
I updated the answer, I forgot that .Location returned the full path to the assembly. That update should fix it to remove the XX.exe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.