-1

I am trying to seed some files so we can programmatically expand our application to new servers when we need to, and lessen the load/documentation needed when training new devs.

So the files exist within the project, but there are other applications that utilize the files and for reasons beyond my control they have to be made available on the disk in a folder that we already create at the startup of our application.

I have tried a few ways to access the files to do a File.Copy with no success. Here is some of the example code of what I've tried

Directory.CreateDirectory(@"C:\appImages");
File.Copy(Path.Combine(Environment.CurrentDirectory, @"Content/Images/someFile.jpg"), @"C:\appImages\someFile.jpg", true);

and

Directory.CreateDirectory(@"C:\appImages");
File.Copy(Path.Combine(Directory.GetCurrentDirectory()), @"Content/Images/someFile.jpg"), @"C:\appImages\someFile.jpg", true);

It's worth mentioning that I'd like this to work when running the application from the exe, and also when Debugging the project locally even if I have to add more code to handle this.

gabessdsp
  • 1
  • 3
  • Just as a general note, if you use a backslash in strings to represent actual backslashes they must be escaped. So it might be you just have bad paths here. I.e., see [how-do-i-write-a-backslash-in-a-string](https://stackoverflow.com/questions/18532691/how-do-i-write-a-backslash-in-a-string) So your problem might be to amend `"C:\appImages\someFile.jpg"` as `@"C:\appImages\someFile.jpg"` – topsail Jun 09 '23 at 20:37
  • @topsail thank you, I amended my question with these edits but my actual code does have the @ sign. I wish my questions didn't always get downvoted. Any ideas what I can do to make this one better? – gabessdsp Jun 10 '23 at 21:02
  • 1
    I think you've already answered your last question. Don't post code with obvious errors in it that will cause it to fail. In this case, it would also help to know what you mean by "it doesn't work" ... are there errors? You should also do some basic debugging on your own - what is the value of `Environment.CurrentDirectory` - is it the correct path you mean to use? Does the folder get created for the target file? Are you stepping through the code - what are you discovering when you do that? – topsail Jun 10 '23 at 21:11
  • One thing to keep in mind: write access to the base C:\ drive and system drives require administrator access by default. So these copy operations _should_ fail, unless you have your users run as an administrator... which is someone worth doing the work to avoid. – Joel Coehoorn Jun 10 '23 at 23:40

1 Answers1

0

I found out, you have to use AppDomain.CurrentDomain.BaseDirectory to do what I am trying to do. I had tried:

  • Environment.CurrentDirectory.ToString()
  • Directory.GetCurrentDirectory().ToString()
  • Assembly.GetExecutingAssembly().Location
  • Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName.ToString()

To no avail.

using System.IO;

// Get the base directory of your project
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

// Specify the relative path of the source file within your project
string relativePath = "path/to/source/file.txt";

// Combine the base directory and relative path to get the full path
string sourceFilePath = Path.Combine(baseDirectory, relativePath);

// Specify the destination file path on the local hard drive
string destinationFilePath = "C:/destination/file.txt";

// Copy the file
File.Copy(sourceFilePath, destinationFilePath);
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
gabessdsp
  • 1
  • 3