-2

I have a Winforms application and I would like to save my database.mdf to %USERPROFILE% folder. Does anyone know what i need to write to the connection string for this to work? Simply using %USERPROFILE% does not work.

Athanasios Emmanouilidis
  • 2,084
  • 3
  • 18
  • 30

1 Answers1

1

You can refer to this

   String query = "%USERPROFILE%";
   String pathOfUserProfile = Environment.ExpandEnvironmentVariables(query);
   Console.WriteLine(pathofUserProfile);

You will get the path in string. Now simply create your connection string as usual

Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
  • Thanks for the fast answer. It works perfectly. Do you know how I can say to my connection string to create a folder inside %USERPROFILE%? It doesn't let me to create a new folder. – Athanasios Emmanouilidis Sep 01 '17 at 16:15
  • 1
    Please tell me about your error. When you try to create a folder which error did you get – Sahil Manchanda Sep 01 '17 at 16:21
  • System.Data.SqlClient.SqlException: 'Directory lookup for the file "C:\Users\devone\Documents\xxx\xxx.mdf" failed with the operating system error 2(The system cannot find the file specified.). CREATE DATABASE failed. Some file names listed could not be created. Check related errors.' – Athanasios Emmanouilidis Sep 01 '17 at 16:22
  • 1
    try creating a folder first using System.IO.Directory.CreateDirectory("YourDirectoryFullPath"); – Sahil Manchanda Sep 01 '17 at 16:28