MarkPflug
MarkPflug
CC#
Created by Valdiralita on 10/2/2024 in #help
creating a nuget for a custom msbuild task
Task projects should be "netstandard2.0". Visual Studio builds using .net framework, not core, so it wouldn't be able to load a net8 library in msbuild
13 replies
CC#
Created by Valdiralita on 10/2/2024 in #help
creating a nuget for a custom msbuild task
Sounds like you got it figured out then?
13 replies
CC#
Created by Valdiralita on 10/2/2024 in #help
creating a nuget for a custom msbuild task
As the article you linked mentioned, you can't use normal nuget package dependencies in an msbuild package, since there is no "nuget restore" for the msbuild steps
13 replies
CC#
Created by Valdiralita on 10/2/2024 in #help
creating a nuget for a custom msbuild task
Here is a project I created that does what you're looking for: https://github.com/MarkPflug/Sylvan.BuildTools.Resources/blob/main/source/Sylvan.BuildTools.Resources/Sylvan.BuildTools.Resources.csproj This .csproj has all the settings needed to produce a correct package. However, this task has no third-partyh dependencies, which might complicate things a bit.
13 replies
CC#
Created by Valdiralita on 10/2/2024 in #help
creating a nuget for a custom msbuild task
You don't want your assemblies to end up in the lib folder, as that would cause the consuming project to use it at runtime. An MSBuild package should only be loaded at build time. So, you want to have your binaries in a different location, such as under your build folder where your .props/.targets files live. I think you can do this by setting <BuildOutputTargetFolder>build</BuildOutputTargetFolder> in the .csproj for your task.
13 replies
CC#
Created by Jasonnn on 9/9/2024 in #help
✅ Reading/Writing in Excel with C#
I'm the author of a library for reading Excel data. It has a pretty minimal API, and is very specifically for getting data out of Excel, ie it doesn't handle formatting, styles, charts, etc. It essentially provides a DbDataReader over Excel, so is ideal for reading rectangular/tabular data. Though, with a bit of work it can be used to read unusual datasets too. Open source, MIT license, extremely memory/CPU efficient, handles reading .xlsx, .xlsb, and .xls: https://github.com/MarkPflug/Sylvan.Data.Excel Might be too minimal for what you need.
43 replies
CC#
Created by Cryy on 7/25/2024 in #help
Performance question
But that doesn't answer why result is needed in the first example.
32 replies
CC#
Created by Cryy on 7/25/2024 in #help
Performance question
If you need result, why not just sum it in app memory, rather than summing query and avoid three roundtrips to db?
32 replies
CC#
Created by Cryy on 7/25/2024 in #help
Performance question
What are you doing with result? ToList will bring the entire query result back to your application. But, for what?
32 replies
CC#
Created by oJaime on 7/11/2024 in #help
Work with .resx Resource files on vscode
22 replies
CC#
Created by oJaime on 7/11/2024 in #help
Work with .resx Resource files on vscode
Unfortunately, I don't have a public project that uses it. Feel free to ask questions/submit issues on the repo if you need help or run into issues.
22 replies
CC#
Created by oJaime on 7/11/2024 in #help
Work with .resx Resource files on vscode
I've implemented a json-based alternative to .resx files that uses MSBuild code gen, instead of VS design-time code gen. Maybe it would work for you? https://github.com/MarkPflug/Sylvan.BuildTools.Resources
22 replies
CC#
Created by QuaKe on 6/28/2024 in #help
✅ "You must install or update .NET to run this application"
@QuaKe You might be able to make this work by adding/editing the *runtimeconfig.json for xstyler: https://learn.microsoft.com/en-us/dotnet/core/versions/selection#control-roll-forward-behavior You would need to set RollForward to Major, indicating that even though xstyler was built against .NET 6, that it should be allowed to run on .NET 8. I think the breaking changes since .NET 3.1 have been so minimal that it is extremely unlikely that there would be an incompatibility. In my opinion, all .NET tools should default to Rollforward=Major in the .csproj file, but it looks like xstyler doesn't do that.
6 replies
CC#
Created by Jesse on 6/4/2024 in #help
Handling reading large files in C#
FileStream will open with FileShare.Read, so other file handles can be opened to read the file. If you try to write to it however, I'd expect that to fail while the FileStream is open.
50 replies
CC#
Created by Jesse on 6/4/2024 in #help
Handling reading large files in C#
How long will your "search string" typically be? Your example uses "test", is that expected to be representative?
50 replies
CC#
Created by Jesse on 6/4/2024 in #help
Handling reading large files in C#
I would stay away from the complexity of MemoryMappedFile, unless you expect the files to exceed 2GB. Even then, you'd probably be better off adjusting your algorithm to work in a streaming/buffered approach
50 replies
CC#
Created by Jesse on 6/4/2024 in #help
Handling reading large files in C#
The API you probably want is:
byte[] bytes = File.ReadAllBytes(filepath);
byte[] bytes = File.ReadAllBytes(filepath);
50 replies
CC#
Created by Jesse on 6/4/2024 in #help
Handling reading large files in C#
Here is your original code annotated to explain why you are seeing so much memory usage:
C#
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
// not sure why you are creating a binaryReader, as you aren't using it...
using (BinaryReader br = new BinaryReader(fs))
{

byte[] bytes = new byte[0];
using (MemoryStream test = new MemoryStream())
{
// This will copy the entire file into the memory stream.
// MemoryStream will dynamically grow, by increasing the internal buffer by 2x
// every time space is exhausted. This means that you'll ultimately use about 2x the memory
// when reading the entire file.
fs.CopyTo(test);
// This allocates a brand new array of exactly the right size and copies
// the bytes from memory stream's intenal buffer to the new array.
bytes = test.ToArray();
}

// The rest of this code is opaque to me, but the "positions" array could grow quite large
// if there are a lot of matches

byte[] searchBytes = Encoding.UTF8.GetBytes("test");
List<long> positions = new List<long>();

foreach (long pos in Extensions.SearchStringInBytes(bytes, searchBytes))
{
positions.Add(pos - 4);
}
}
C#
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
// not sure why you are creating a binaryReader, as you aren't using it...
using (BinaryReader br = new BinaryReader(fs))
{

byte[] bytes = new byte[0];
using (MemoryStream test = new MemoryStream())
{
// This will copy the entire file into the memory stream.
// MemoryStream will dynamically grow, by increasing the internal buffer by 2x
// every time space is exhausted. This means that you'll ultimately use about 2x the memory
// when reading the entire file.
fs.CopyTo(test);
// This allocates a brand new array of exactly the right size and copies
// the bytes from memory stream's intenal buffer to the new array.
bytes = test.ToArray();
}

// The rest of this code is opaque to me, but the "positions" array could grow quite large
// if there are a lot of matches

byte[] searchBytes = Encoding.UTF8.GetBytes("test");
List<long> positions = new List<long>();

foreach (long pos in Extensions.SearchStringInBytes(bytes, searchBytes))
{
positions.Add(pos - 4);
}
}
50 replies
CC#
Created by Davide Dunne on 2/20/2024 in #help
Blazor: CSV exported is empty
Try this:
C#
private Stream GetFileStream()
{
var memoryStream = new MemoryStream();
using (var streamWriter = new StreamWriter(memoryStream, leaveOpen: true))
using (var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture))
{
csvWriter.WriteRecords(objects);
}

// Reset memory stream
memoryStream.Position = 0;

return memoryStream;
}
C#
private Stream GetFileStream()
{
var memoryStream = new MemoryStream();
using (var streamWriter = new StreamWriter(memoryStream, leaveOpen: true))
using (var csvWriter = new CsvWriter(streamWriter, culture: CultureInfo.InvariantCulture))
{
csvWriter.WriteRecords(objects);
}

// Reset memory stream
memoryStream.Position = 0;

return memoryStream;
}
8 replies
CC#
Created by Davide Dunne on 2/20/2024 in #help
Blazor: CSV exported is empty
Second, you might run into issues because you aren't flushing/disposing anything. CsvWriter and even StreamWriter might have internal buffering that would prevent everything from arriving in the MemoryStream until they are flushed, or closed. Be aware, that disposing the StreamWriter will close the MemoryStream by default, there is a constructor overload that accepts a boolean to tell it to leave the stream open.
8 replies