I have a code.cs file that I compile with the following command line:
"%ProgramFiles(x86)%\MICROS~3\2017\ENTERP~1\MSBuild\15.0\Bin\Roslyn\csc.exe" ^
/target:library /out:fx1.dll fx1.cs ^
/reference:"C:\blah\blah\Microsoft.JScript.dll" ^
/reference:"C:\astor\loads\better\ease\zog.dll"
Is there a practical way to specify those options inside the fx1.cs file ?
This is mainly C#, but what about other .Net languages like Vb.Net and the others? Is there at least one language that can specify compiler parameters in a self-contained file?
Update: I also see there are answer files for csc, but they lack (or I can't see) enough flow control to embed one in a cs file.
The solution I came up with as baseline answer for this question is to turn the C# file into a "polyglot" *.cs.bat file that runs the full compile command on top of the C# source code.
/*? 2>NUL & #echo off
echo.
echo COMPILING...
"%ProgramFiles(x86)%\MICROS~3\2017\ENTERP~1\MSBuild\15.0\Bin\Roslyn\csc.exe" ^
/target:library /out:fx1.dll fx1.cs.bat ^
/reference:"C:\blah\blah\Microsoft.JScript.dll" ^
/reference:"C:\astor\loads\better\ease\zog.dll"
PAUSE
GOTO:EOF REM */
// C# program...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
// blah...
To compile I can just run fx1.cs or fx1.cs.bat. That certinly is practical.
This may be useful but the downsides are at least these:
text highlighting is lost in the various editors that have it for *.cs files... If only there was a (easy) way to run *.cs files as batch (type file | cmd seems to ignore PAUSE, EXIT /B, EXIT, GOTO:EOF)
The compiler path is hardcoded (but I think it can be determined with more batch fiddling)
It tries to run /*.exe, so the first line always gives me an error because I found no way to avoid it being run, so I can only put a ? in there (so it should not be a valid path ever) and hide it with error redirection.
While it is unpractical to edit in an IDE, I think this may be an easy format for distribution of simple utilities.
I'm not very good at this... so if someone has better ideas (for the polyglot route) please improve this answer by commenting or editing it.
Related
I'm looking to refactor a bat file into a C# console application. The main task of this bat file is to move and rename files using wildcards. I cannot find if there is anything in C# System.IO namespace (or other namespace) that can accomplish what I am trying to do.
Example input file:
D:\Testing\SrcDir\Input-20210318123001.txt
Desired output file:
D:\Testing\DestDir\Output-20210318123001.txt
Here is an example of the existing commands run that I am looking to convert to C#:
ren D:\Testing\SrcDir\Input-*.* Output-*.*
copy D:\Testing\SrcDir\Output-*.* D:\Testing\DestDir\
del D:\Testing\SrcDir\Output-*.*
My first thought was to simply use the System.IO.File.Move method. But this does not support wildcard usage. Neither does the System.IO.File.Copy method.
I'm trying to avoid using regex if possible (I have little to no experience with regex). I'd like to avoid rolling my own here if there is a simple solve for this that I have missed in my searching. Is there a good way to refactor this functionality into C#?
this might be a wrong place to ask this (but I think its right place since it involves programming).
So I got a raspberry pi zero for our school project. I SSH'd into it to check out what it can do with it. I made some research about how to use the GPIO pins on this card.
Basically:
$ echo 17 > /sys/class/gpio/export
$ echo out > /sys/class/gpio/gpio17/direction
$ echo 1 > /sys/class/gpio/gpio17/value
$ echo 17 > /sys/class/gpio/unexport
Enables GPIO pin 17 and writes digital 1 into it and 'unexport's it, no root is needed.
I also wanted to try out some of the languages in this card. I tried python, C# and Rust without a problem (even though rust compiles very slow, it works). So I started using my favourite language C# with mono. Installing it and compiling a basic program wasn't a big deal, It works.
So I write this:
using System;
using System.IO;
namespace Program
{
public static class Program
{
public static void Main()
{
if (Directory.Exists("/sys/class/gpio/gpio17/"))
File.WriteAllText("/sys/class/gpio/unexport", "17");
File.WriteAllText("/sys/class/gpio/export", "17");
File.WriteAllText("/sys/class/gpio/gpio17/direction", "out");
File.WriteAllText("/sys/class/gpio/gpio17/value", "1");
}
}
}
Basically, if it finds the 17 pin open, 'unexport' it then re-export it, set as output and write digital 1.
Compilation:
mcs program.cs -out:program.exe -debug && ./program.exe
Output:
Unhandled Exception:
System.UnauthorizedAccessException: Access to the path "/sys/class/gpio/gpio17/direction" is
denied.
What? How? It works with sudo mono ./program.exe and no it doesn't with mono ./program.exe
Sure, I can always use wiringPi or python but I am curious about this one and couldn't find an answer. It doesn't make sense to me. /sys/class/gpio/gpio17 is a symbolic link and I tried to access to original path too with no luck.
What might be the problem here?
Guesses:
You unexport it first, then export it and immediately access it, I had in the past several time problems on some OS e. g. deleting a file and immediately creating it again, add a waiting time (not really clean code, but if it works)
check the difference of the files/folders with linux command "ls -la" or more
call the shell command, that worked, from C# via e. g. Process class
Some few times Mono did not work like C# on Windows, a workaround had to be found
try to open a stream to these files and leave them open? Or even a pipe is possible to these files?
I would like to find all file paths that are not filtered by a .gitignore (or any nested .gitignore files within sub-directories) using C#. This is similar to the question here with regard to PHP. I'm wondering if someone knows if this code had already been made available (in C#) somewhere online.
UPDATE: To answer what I want this for, it is so I can run my own little periodic backup of my source files for certain projects (zipping the result), for added peace of mind. The hard part is getting a robust .gitignore parser to get the filtered file paths (and exclude the others), without wanting to become too embroiled in learning that spec if someone else already has done it for me.
Well, the best way to parse .gitignore files (and the other files Git uses, such as $GIT_DIR/info/exclude) is to get Git to do it for you. :-) (In your case, most cases in fact, this does involve executing a git subprocess.)
git check-ignore
The git check-ignore command can be used to detect which files are ignored and why. The --non-matching option makes it tell you about files that are not ignored as well, though since it still tells you about ignored files, too, and in a special format, you'll need to do a little bit of further work to get a simple list of non-ignored files. This Bourne shell function does the trick:
find_nonignored() {
find . -path ./.git -prune -o -print \
| git check-ignore --verbose --non-matching --stdin \
| sed -n -e 's,\t./,\t,' -e 's,^::\t*,,p' \
}
How It Works
The find command finds all files in and below the current working directory, which should be somewhere in the tree you're trying to filter. We exclude the top-level .git subdirectory and everything under it from the output, if present; /.git/ is not in a typical .gitignore file because Git ignores it automatically and thus is is normally considered "not ignored" by git check-ignore.
git check-ignore will print out --non-matching files only in --verbose mode because it's only in that mode where it prints out the extra information that would tell you if the file is ignored or not. (It always prints ignored files.) The paths come out one per line in the format
source:linenum:pattern<TAB>path
The colon-separated fields are information about what caused the path to be ignored (such as a line in the .gitignore file) and will be empty if the file is not ignored.
The sed command then filters the output to show only the paths of the ignored files. The -n option tells it not to print out the input lines by default. The first substitution pattern replaces <TAB>./ with just <TAB>, removing the leading ./, for purely aesthetic reasons. The second substitution does the real work, removing any ::<TAB> (indicating no "ignore" information) that starts a line and, if that substitution happened, printing what's left of the line which is a non-ignored path.
You can filter this further to do additional processing; I built this for a script that does markdown checking along these lines:
markdownlint $(find_nonignored | grep '\.md$')
Notes
This code includes untracked files (i.e., have never been added to the Git repo or staged) in the output, which is usually what you want. (Test systems, for example, should still check new files even before they've had git add run on them.) Beware that other solutions involving git ls-files and the like usually don't do this.
The above code relies on using GNU sed, which interprets \t as a tab. If you're using BSD sed (such as on MacOS) you probably need to tweak this slightly. Check the comments to see if someone has a hint for this.
All the code here breaks on paths with spaces or other "unusual" characters; it needs to be modified in several places (such as using -print0 with find) to fix this. I do not address issues like this here in order to keep the explanation simple. I also leave for others the generalization of the function to work on arbitrary paths rather than just the current working directory.
It's difficult to make suggestions without knowing exactly what you want to do with the list (use it in a build script, process the files in some way, just view them on a UI, etc.)
I couldn't find one in C#, but this JavaScript gitignore parser doesn't have a lot of code to convert and it exposes both an accepts and a denies method to get a list of included or ignored files. It is fairly well documented, has tests, and the regular expressions it uses would work just as well in C# as they do in JavaScript.
This answer would work from C#, provided you have Git installed on the machine where your C# code is running.
Also note that the Git Source Control Provider plugin for Visual Studio provides the list right in the IDE, along with the ability to check boxes and commit certain files together and a lot of other functionality that is difficult to do on the command line.
NOTE: The Git Source Control Provider is open source (written in C#) and you can view the source here, but it may be much more involved to reverse engineer than the JavaScript project.
For those looking for a C# library, you can check this out as well.
.gitignore based parser implemented in C# according to the .gitignore spec 2.29.2. The library is tested against real git status outputs. The tests use LibGit2Sharp for that.
https://github.com/goelhardik/ignore
It's kind of a port of other open source libraries and so far looks like it works well for my other projects.
I recently needed to embed Visual Source Safe history in the source code of a new project, according to some examples we have in older project, where the history appears where the coder has placed certain comments. The final result is something like:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
//$Archive: /UserManagementSolution.root/UserManagementSolution/UserDAL/Tems.cs $
//$History: Tems.cs $
//*
//* ***************** Version 3 *****************
//* User: michaelc Date: 1/31/13 Time: 9:44a
//* Updated in $/UserManagementSolution.root/UserManagementSolution/UserDAL
//
//
namespace UserDAL
{
public class Tems
The Version along with the two lines following it are placed by VSS. I was aware that VSS placed the history according to where one places the lines:
//$Archive: $
//$History: $
//*
Note that VSS also places the VSS file information in the $Archive and $History paras. Just use dollar signs as shown to tell it where this information goes.
However, when I actually checked in the code (about 20 C# files), VSS put the history in WITHOUT comment characters, and thus broke the build. This required some annoying rework to comment the lines. The question is, how do I make VSS comment the history when it puts it in?
I spent some time pondering the problem, then suddenly remembered that there was some coding required in the srcsafe.ini file to tell VSS put the history in commented lines.
I located the srcsafe.ini file of a project that was behaving as desired, and compared it with the one for my new project. I found the following lines in the correctly-behaving .ini file:
Keyword_Masks = *.cs, *.sql
Shadow =
[Keyword Comments]
*.cs = "//"
*.sql = "--"
The Keyword_Masks and Shadow keywords are placed in the .ini file after the UseHelperService. I suppose the Keyword_Masks (with comma-separated values) tell VSS which file types to place the history (I don't know what Shadow signifies, but since it occurs in all of our projects I didn't want to omit it and find out it caused something subtly wrong later).
The new [Keyword Comments] section occurs immediately afterwards (and before the [Timezone] section. This obviously tells VSS which characters to use for line comments, and for which file type.
And that did it!
Note that if you're using Visual Basic, this will be coded as *.vb. Obviously. And you don't have to include Sql; in this case our VSS project happened to contain solution and project files with stored procedures that we were also keeping in VSS.
I need to create a resource file for a .net project (by hand) and compile it using the ResGen.exe tool provided by the .NET framework. I can't find any documentation for this. I need to write the resource file by hand because I'm in a situation where I don't want to download/buy extra tools (like VS) to generate this resource file, and also I feel more productive through the command-line (helps me understand how things really work).
So I need to write a resource file by hand to store an ICON in the executable and use it from within my program. I would also like to use this icon to represent my executable in Windows Explorer.
Any references would be great!
Visual C# Express Edition will do what you want for free. If nothing else you can download that, create the resource file and then use that as a subject for your admirable curiosity about 'how it really works'. This may also save you some time in manual experimentation to get it right the first time around.
These 2 links in conjunction provide information on using that tool to create and embed an icon file, it seems specific to C#. Of course i'm guessing at your full intention, let me know if this points you in the proper direction.
http://www.xtremedotnettalk.com/showthread.php?t=75449
specifically there is a post which states;
I think you should first create a *.resources-File from the Icon with the tool named "Resgen.exe"...
resgen App.ico App.ico.resources
the next step would be compiling...
csc /t:winexe /out:Keygen.exe /res:App.ico.resources /r:Crypto.dll /win32icon:App.ico Keygen.cs AssemblyInfo.cs
I'm sure you were here already.
http://msdn.microsoft.com/en-us/library/ccec7sz1(VS.80).aspx
You should check this link:
http://msdn.microsoft.com/en-us/library/ekyft91f.aspx
It explains what formatter is used and gives some code samples to generate one from code. You could then write a small wrapper app that you can call from the command line. No downloads needed!