1

I have a C# file that manipulates some files in a given directory.

The files I want it to manipulate are files that lie within the working directory of my project and need to be updated when the build becomes out of date. How do I run this simple C# source as a pre-build event?

Ideally, I want to be able to compile it for cross-platform. Currently, the target project is a .NET Core project.

Do I compile the small C# source as a single executable and then place that in the target project? That seems inefficient. Is there a way to do it where I put this small C# source into a project within the same solution as my target project, then run that new project as a pre-build event?

kpaul
  • 355
  • 6
  • 16
  • What is pre-build? – Vy Do Feb 22 '20 at 03:38
  • 2
    Does it need to be pre-build? If post-build is okay, you could make the second project a dependency of the first, so it automatically gets built when you build your main project, and then you can call the generated exe to manipulate the files in a post-build event – Chronicle Feb 22 '20 at 03:53
  • 1
    See [here](https://learn.microsoft.com/en-us/visualstudio/ide/how-to-specify-build-events-csharp?view=vs-2019#example) for an example to get you started. The difference with your version is that the second project will reference the first, so you don't have to copy the executable somewhere, you can just rely on the output directory of the build. – Chronicle Feb 22 '20 at 04:08

1 Answers1

-2

Maybe you are new to .NET Core but have other programming experience. If so, continue reading, it's about a mental bias you may need in order to develop using .NET Core. I can assure you it's for good.

I have a C# file that manipulates some files in a given directory.

C# file cannot manipulate anything, it's just some C# code. In order to do anything else then edit, delete, or compile it you have to make it part of a project, compile the project and run it, say, applying it to the folder you need in your case.

So, yes, you need an executable in order to run it.

The files I want it to manipulate are files that lie within the working directory of my project and need to be updated when the build becomes out of date.

.NET Core handles all these - you shouldn't do anything. Search for Application Lifecylce Management (ALM) and get familiar with it.

Ideally, I want to be able to compile it for cross-platform.

.NET Core is cross-platform by itself, you shouldn't care about it. It's the java's dream "compile once run everywhere" come true.

So, as a bottom line - keep your life easier not thinking of such fictitious issues - someone has already taken care of all these.

Unless you have something else in mind, which unfortunately, I cannot figure out from your question.

And a final remark, or a word of warning if you will - .NET is all but an amateur set of tools. You should spend a great deal of time on it, but you'll be much grateful at the end.

HTH

Alexander Christov
  • 9,625
  • 7
  • 43
  • 58