Surihia
Surihia
CC#
Created by Surihia on 1/22/2024 in #help
A small help in understanding structs:
I am learning how to use structs and have a question. for this question I decided to write a small test program by using some structs.
Inside Program.cs
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Program
{
static void Main(string[] args)
{
var testHoldStruct = new TestHold();
var testHoldStruct2 = new TestHold2();

testHoldStruct.val1 = 100;
testHoldStruct.val2 = 500;

testHoldStruct2.val2_1 = 1000;
testHoldStruct2.val2_2 = 5000;

Class1.ProcessTest(testHoldStruct, testHoldStruct2);

Console.WriteLine("");
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Program
{
static void Main(string[] args)
{
var testHoldStruct = new TestHold();
var testHoldStruct2 = new TestHold2();

testHoldStruct.val1 = 100;
testHoldStruct.val2 = 500;

testHoldStruct2.val2_1 = 1000;
testHoldStruct2.val2_2 = 5000;

Class1.ProcessTest(testHoldStruct, testHoldStruct2);

Console.WriteLine("");
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
Inside Class1.cs file:
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Class1
{
public static void ProcessTest(TestHold testHoldStruct, TestHold2 testHold2Struct)
{
Console.WriteLine(testHoldStruct.val1);
Console.WriteLine(testHoldStruct.val2);

Console.WriteLine(testHold2Struct.val2_1);
Console.WriteLine(testHold2Struct.val2_2);
}
}
}
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Class1
{
public static void ProcessTest(TestHold testHoldStruct, TestHold2 testHold2Struct)
{
Console.WriteLine(testHoldStruct.val1);
Console.WriteLine(testHoldStruct.val2);

Console.WriteLine(testHold2Struct.val2_1);
Console.WriteLine(testHold2Struct.val2_2);
}
}
}
Inside Class2.cs file:
c#
namespace TestStuff
{
internal class Class2
{
public struct TestHold
{
public int val1;
public int val2;
}

public struct TestHold2
{
public int val2_1;
public int val2_2;
}
}
}
c#
namespace TestStuff
{
internal class Class2
{
public struct TestHold
{
public int val1;
public int val2;
}

public struct TestHold2
{
public int val2_1;
public int val2_2;
}
}
}
Am I correct in assuming that when I am calling ProcessTest method from Program.cs file, a copy of the struct object is created in memory when Class1 is being processed ? I assume that if I use a class instead of a struct for ProcessTest method, I would only be passing it as a reference to the class object and in that case, a copy of the object isn't created when Class1 is being processed
8 replies
CC#
Created by Surihia on 5/5/2023 in #help
❔ How do I use the System.Windows.Forms namespace in a .net core compiled console app ?
I want to use the open file dialog box and the messagebox functionalities from this namespace in my console app. the former is for selecting a path of a particular file by which my app does few file related operations and the messagebox is used for displaying success and error messageboxes.
36 replies
CC#
Created by Surihia on 3/31/2023 in #help
❔ Increase Hash computing speed.
So I am trying to make a simple app for my use case that computes SHA256 of two files. right now I have this following code which works but is extremely slow:
static void Main(string[] args)
{
var inFile_1 = args[0];
var inFile_2 = args[1];

Console.WriteLine("Computing hash....");
using (FileStream fs = new FileStream(inFile_1, FileMode.Open, FileAccess.Read))
{
using (SHA256 mySHA256 = SHA256.Create())
{
fs.Position = 0;
byte[] HashBuffer = new byte[4096];

HashBuffer = mySHA256.ComputeHash(fs);

Console.WriteLine(BitConverter.ToString(HashBuffer).Replace("-", ""));
Console.ReadLine();
}
}
}
static void Main(string[] args)
{
var inFile_1 = args[0];
var inFile_2 = args[1];

Console.WriteLine("Computing hash....");
using (FileStream fs = new FileStream(inFile_1, FileMode.Open, FileAccess.Read))
{
using (SHA256 mySHA256 = SHA256.Create())
{
fs.Position = 0;
byte[] HashBuffer = new byte[4096];

HashBuffer = mySHA256.ComputeHash(fs);

Console.WriteLine(BitConverter.ToString(HashBuffer).Replace("-", ""));
Console.ReadLine();
}
}
}
I have only set it to get the hash for one file. my issue is the speed of the hash computing is extremely slow. is there any way by which I can increase the speed ?
35 replies
CC#
Created by Surihia on 2/26/2023 in #help
❔ WPF+Win32 interop issue
"Dispatcher processing has been suspended, but messages are still being processed." A friend of mine is having this problem with C# and as he is unable to directly communicate here due to an issue with his phone number, I am asking on behalf of him here for a solution to his C# problem. he has explained his problem a lot more clearly in this stack overflow page: https://stackoverflow.com/questions/75568128/wpfwin32-interop-issue-dispatcher-processing-has-been-suspended-but-messages Can anyone here help in solving this issue ? I will try my best to convey and replay answers to any questions you have on the code to him.
2 replies
CC#
Created by Surihia on 2/9/2023 in #help
❔ What does this exception mean?
(Sorry for reposting as I already put it in the help chat and don't want it to get buried) System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' Basically I am passing a File Path as a string to another classfile and this classfile uses a dll and reads the File provided to a array. as soon as it gets to the code that do a value change onto the file, this exception is thrown. The File in question is not accessed or being used by any other method and the path is correctly seen in the class file too.
11 replies
CC#
Created by Surihia on 2/6/2023 in #help
❔ Writing a existing color palette values to a existing raw image pixels to color it
I have the color palette values stored separately in a different file away from the image raw pixels file. is there a way by which I can write in the color values from the color palette file to the image and color it ?
17 replies
CC#
Created by Surihia on 12/14/2022 in #help
✅ Getting a string from a text file and get the whole line containing the string
I am making a app where I want to get the filename of a video file and then check if the filename exists in a text file. if it exists, then I want to get the line containing that string and I have few problems with this. The video filename is "VIDEO_PRODUCT_AD01_AVI.mp4" This very filename is stored in the text file as this string: "video_product_AD01_avi.mp4" This filename is usually stored along with the file path in this way along with a lot of video filenames in this text file.
20797607 ..\Video\Products\video_product_AD01_avi.mp4
20797607 ..\Video\Products\video_product_AD01_avi.mp4
Now I know how to get the filename as a string from the video file but then matching that filename with the respective filename string in the text file, is where I am facing problems. I don't want to change the video filename or the filename string in the text file. is there any solution to this ?
24 replies
CC#
Created by Surihia on 11/23/2022 in #help
✅ Finding the position of a string in a binary file
I have a binary file and in it I want to search for a specific string which I will pass via a textbox. if that string exists in the binary file, then I want to get the byte position of the first letter from the string in that file.
36 replies
CC#
Created by Surihia on 11/13/2022 in #help
✅ How to terminate reading a set of string chars when a null character is detected.
27 replies
CC#
Created by Surihia on 11/8/2022 in #help
Creating a file with a fixed size
I am writing a video splitter app for my personal storage and would like some help. What my app does is split a video file into multiple tiny files. I will call each tiny file a piece here. The issue I am facing is creating a new file piece with a custom size. I want to specify a size value for a piece and that size value is given via a argument. Here is a rough example of my argument:
videofile.mp4, 50, pieceName
videofile.mp4, 50, pieceName
I can create a new file with a filestream, and open the video in another filestream. but the fs.CopyTo() method doesn't have a parameter to copy an x amount of bytes and so I can't tell it to copy 50 bytes of data from one filestream to another filestream. How do I handle this problem ?
26 replies
CC#
Created by Surihia on 11/7/2022 in #help
✅ Getting a string value from a set of strings stored in a text file
I have a text file which has strings written in this following pattern:
Name:Age:DOB:Designation
Name:Age:DOB:Designation
With data its as follows. imaginary names:
Jon:30:99:Editor
Jon:30:99:Editor
These strings are all in each line and I want to get the Name, Age, DOB, and Designation strings as separate strings so that I can use them as variables in my program to parse. How do I go about in doing this ?
11 replies
CC#
Created by Surihia on 10/14/2022 in #help
✅ Using a method that is running on another thread
I am making a program that does few operations to video files. on clicking a button, an dialog box is opened in which the user has to select the video file. Once the video gets selected in the dialog box, the operation will begin. here is where I encounter a problem. I want to write messages as each operation is done performing or when an operation is about to perform to a listbox in my GUI app. If this was a commandline app, I can just use Console.Writeline with my message strings but in a GUI app, I can't do something similar and so I decided to use the listbox command and here is where my issue begins. The issue is that all the messages will appear only when my program's whole video operation code is done performing and that defeats the whole point of using messages to tell the user that something is being performed when the operation is being done. I heard that I have to multi thread my app and so decided to put my whole video related code to another thread and then use a method to call the strings from my video extraction thread. I encounter a exception with this one stating that I can't access a method which is in another thread. Now the first message I write will be Parsing Video file.... and this message will appear once the video is selected in the dialog box and is being opened in a filestream. My code below should show as to how I am using a method to call the messages.
15 replies
CC#
Created by Surihia on 9/6/2022 in #help
Embedding dlls into a compiled exe [Answered]
I have few dll packages that I am using with my app and I want to embed them all inside a single compiled executable file. I am using the following packages:
System.Buffers
System.Memory
System.Buffers
System.Memory
Now I have added the dll files by right clicking on my Project in the Solution explorer and by selecting the Add item option. the compiled app is larger as a result of this, but when I try running the app, it throws exceptions that the respective dll file is not present or unable to be loaded.
40 replies
CC#
Created by Surihia on 8/30/2022 in #help
Is there a way to determine the free space on a drive by using a text file ?
I have a path to a folder which is stored in a text file: D:\Projects\Videos\current\ I want the program to take the drive letter from the text file and then show the space on the console. Currently my code is as follows which only shows the free space on my root drive C.
static void Main(string[] args)
{
var list = new List<string>();
var DrivepathTxtFile = new FileStream("LocatedPath.txt", FileMode.Open, FileAccess.Read);


StreamReader reader = new StreamReader(DrivepathTxtFile);

string DrivePath;
while ((DrivePath = reader.ReadLine()) != null)
{
list.Add(DrivePath);
}

DriveInfo[] Drives = DriveInfo.GetDrives();

foreach (DriveInfo d in Drives)
{
Console.WriteLine(d.TotalFreeSpace);
Console.ReadLine();
}
static void Main(string[] args)
{
var list = new List<string>();
var DrivepathTxtFile = new FileStream("LocatedPath.txt", FileMode.Open, FileAccess.Read);


StreamReader reader = new StreamReader(DrivepathTxtFile);

string DrivePath;
while ((DrivePath = reader.ReadLine()) != null)
{
list.Add(DrivePath);
}

DriveInfo[] Drives = DriveInfo.GetDrives();

foreach (DriveInfo d in Drives)
{
Console.WriteLine(d.TotalFreeSpace);
Console.ReadLine();
}
I copied the code straight from the MS page and this obviously does not work as the code is picking my C Drive's space.
26 replies