1

I have a text file here:

enter image description here

In it I have the contents of a SQL script that I want to run. Previously, I had the file in a network path, however now I want to text file to live in the solution as it is in the picture above.

I have this method that used to call File.ReadAllText() on the network path of the SQL script which then stored the text as the script and then executed the SqlCommand: ExecuteScriptFromSolution Method

The question that I have is how can I refer to TextFile1.txt in the File.ReadAllText() method so I can maintain the .txt files in my solution versus a network drive? Any help is appreciated.

JOberloh
  • 1,026
  • 2
  • 11
  • 20
  • 4
    Please post code as text, not as a screenshot. You might also want to obfuscate your employer. – Jamiec Aug 22 '19 at 14:02
  • ReadAllTest(string filename) accepts a filename located any place. You need to include both the path and filename. The path can be local or network drive. – jdweng Aug 22 '19 at 14:05
  • 1
    @jdweng being that this is a selenium project I'm guessing the OP wants it as an embedded resource. Which means you need to read from a stream not a file path of course. – Jamiec Aug 22 '19 at 14:06

2 Answers2

1

You can set the build process to copy this text file to the output directory (as explained here).

Then you can access it like:

script = File.ReadAllText($"{AppDomain.CurrentDomain.BaseDirectory}/TextFile1.txt");
andresantacruz
  • 1,676
  • 10
  • 17
0

If I understood correctly, you want to include or exclude some files from your publish.

  • You should use CopyToPublishDirectory and then to reach \bin folder
    Directory.GetParent(Environment.CurrentDirectory).FullName
    
  • You can create a custom folder and include your file. Then;
    Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\CustomFolder\\CustomFile.txt"
    
  • or you may make a quick search include exclude file from publish .net