Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dll file that for now uses a .txt file as input that is located in the debug folder of my application. I want to replace this .txt file with text as submitted inside of a textbox within my application.

I'm still new to MVVM and WPF, and managed to get the dll functions working. I am however struggling with replacing the .txt file with textbox input. If anyone could help me out or point me towards the right path that would be greatly appreciated.
For clarification I want the method to read Textdetail (the text inside the textbox) instead of the contents of the file.


Method and string names have been changed as I cant share too much.

Inside the class


C#
public string Textdetail { get; set; }

            [DllImport("Blabla.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    
            public static extern void Method1 (
                string string1,
                StringBuilder string2,
                StringBuilder string3);
    
            public static void Createtxt(object data, string fileName)
            {
                var stream = new FileStream(fileName, FileMode.OpenOrCreate);
                using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                {
                    writer.Write(data);
                    writer.Close();
                }
            }
            public static void ExecuteProject()
            {
                string file = "Dependendfile.txt";
                int capacity = 1000000;
                int maxCapacity = 999999999;
                StringBuilder string1 = new StringBuilder(capacity, maxCapacity);
                StringBuilder string2 = new StringBuilder(capacity, maxCapacity);
                LogixConvertRungs(File.ReadAllText(file), string1, string2);
                CreateTxt(string1.ToString(), "File1-" + "Demo"+ ".txt");
                CreateTxt(string1.ToString(), "File2-" + "Demo"+ ".txt");
            }


the xaml textbox text

C#
<TextBox x:Name="txt_replacefilewithtextfromthis" 
                 TextWrapping="Wrap" 
                 AcceptsReturn="True"
                 FontFamily="Corsiva" 
                 FontSize="11"
                 Text="{Binding Textdetail, Mode=TwoWay}"/>


What I have tried:

I've tried looking up multiple tutorials, but cant figure out how to do it. The application either ignores the textbox input or gives off different erros
Posted
Updated 1-Jun-21 4:17am

1 solution

If you want to replace the contents of a text file, use File.WriteAllText[^].

However, your application is probably going to be running from a folder in the "Program Files" path. In that case, a standard user will not have permission to modify any files in the application folder. You would need to store the file somewhere else instead:
Where Should I Store My Data?[^]

Also note that if your project is set to always copy the text file to the output directory, the file in your Debug folder will be overwritten every time you run your project, so it might look like the data isn't being saved.

If you want help to fix an exception, then you need to provide the full details of that exception. Just saying it produces different errors tells us nothing.
 
Share this answer
 
Comments
Member 15183001 1-Jun-21 10:24am    
Hi Richard, I think you misunderstand my problem.

I do not want to replace the contents of a text file. I want to completely get rid of the text file.

For now the dependend text file is located inside of my debug folder. What I want to do is make the executeproject method dependend on the text inside the textbox of my application (Textdetail).

I'm fully aware of the output being replaced everytime, for now that works for me though. It's the input (Dependendfile.txt) that I wish to modify (with Textdetail as inside of my textbox).
Richard Deeming 1-Jun-21 10:25am    
So why are you dealing with a text file at all? Delete the text file, and change your code to read the text from whatever your textbox is bound to instead.
Member 15183001 1-Jun-21 10:28am    
Which is what I'm struggling with to do. I understand it's likely something basic, but I haven't managed to get it to work. I've tried replacing LogixConvertRungs(File.ReadAllText(file), string1, string2); with LogixConvertRungs(File.ReadAllText(TextDetail), string1, string2); but that turns up empty
Richard Deeming 1-Jun-21 10:26am    
Assuming your C# code is all in the same view-model:
LogixConvertRungs(Textdetail, string1, string2);
Member 15183001 1-Jun-21 10:29am    
That brings up an "An object reference is required for the nonstatic field or property" error. The code is all in the same view model

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900