Click here to Skip to main content
16,016,140 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following function in vb.net that I want to convert it in C# in order to create a set up file for trial and licenced version of application.
I used several converters because I was facing converting issues.

VB
<pre> 
<pre> Public Sub ReWriteToFile(ByVal Name As String, ByVal Value As String)
        Dim File As IO.StreamWriter


        Dim fileContents = IO.File.ReadAllText("C:\BackgammonTxt\AppDataFile.txt")
        File =
            My.Computer.FileSystem.OpenTextFileWriter("C:\BackgammonTxt\AppDataFile.txt", True)

        'Dim attributes As FileAttributes = IO.File.GetAttributes("C:\BackgammonTxt\AppDataFile.txt")

        'If Not (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
        '    IO.File.SetAttributes("C:\BackgammonTxt\AppDataFile.txt",
        '                       IO.File.GetAttributes("C:\BackgammonTxt\AppDataFile.txt") Or FileAttributes.Hidden)
        '    'MsgBox("The file is now hidden ")
        'End If
        IO.File.SetAttributes("C:\BackgammonTxt", IO.FileAttributes.Normal)
        IO.File.SetAttributes("C:\BackgammonTxt\AppDataFile.txt", IO.FileAttributes.Normal)

        File.Close()

        Dim FileStr As String = "C:\BackgammonTxt\AppDataFile.txt"
        Dim newline As String = Name & Value.ToString

        If System.IO.File.Exists(FileStr) Then
            Dim lines() As String = IO.File.ReadAllLines(FileStr)
            For i As Integer = 0 To lines.Length - 1
                If lines(i).Contains(Name) Then
                    lines(i) = newline
                End If
            Next
            IO.File.WriteAllLines(FileStr, lines) 'assuming you want to write the file
        End If

        File.Close()
    End Sub




What I have tried:

<pre lang="C#">
<pre> public void ReWriteToFile(string Name, string Value)
        {
            System.IO.StreamWriter File;


            var fileContents = System.IO.File.ReadAllText(@"C:\BackgammonTxt\AppDataFile.txt");
            File = My.Computer.FileSystem.OpenTextFileWriter(@"C:\BackgammonTxt\AppDataFile.txt", true);
            
            System.IO.File.SetAttributes(@"C:\BackgammonTxt", System.IO.FileAttributes.Normal);
            System.IO.File.SetAttributes(@"C:\BackgammonTxt\AppDataFile.txt", System.IO.FileAttributes.Normal);

            File.Close();

            string FileStr = @"C:\BackgammonTxt\AppDataFile.txt";
            string newline = Name + Value.ToString();

            if (System.IO.File.Exists(FileStr))
            {
                string[] lines = System.IO.File.ReadAllLines(FileStr);
                for (int i = 0; i <= lines.Length - 1; i++)
                {
                    if (lines[i].Contains(Name))
                        lines[i] = newline;
                }
                System.IO.File.WriteAllLines(FileStr, lines); // assuming you want to write the file
            }

            File.Close();
        }



I am getting error in
File = My.Computer.FileSystem.OpenTextFileWriter(@"C:\BackgammonTxt\AppDataFile.txt", true);

I want to be able to append data on the file and I don't understand how to change this line of source code to make it work.
I found something like the following but I am not pretty sure if it is right, because we don't have any parameter, as append boolean flag. The syntax is different. I would like to have the right one, with an explanation please. Thank you so much in advanced.
C#
File = new System.IO.StreamWriter(@"c:\test2\test.txt");
Posted

1 solution

The StreamWriter class does have a constructor which takes an append parameter:
C#
public StreamWriter (string path, bool append);
If you don't pass the parameter, it defaults to false, so you will end up overwriting the file instead of appending to it.

However, that method is doing a lot of unnecessary work, since it seems all you want to do is update one line in the file:
C#
public void ReWriteToFile(string name, string value)
{
    const string FilePath = @"C:\BackgammonTxt\AppDataFile.txt";
    if (System.IO.File.Exists(FilePath))
    {
        bool found = false;
        string[] lines = System.IO.File.ReadAllLines(FilePath);
        for (int index = 0; index < lines.Length; index++)
        {
            if (lines[index].StartsWith(name))
            {
                lines[index] = name + value;
                found = true;
            }
        }
        if (found)
        {
            System.IO.File.WriteAllLines(FilePath, lines);
        }
        else
        {
            // TODO: The name was not found in the file; what to do here?
        }
    }
    else
    {
        // TODO: The file does not exist; what to do here?
    }
}
 
Share this answer
 
Comments
Aggeliki Asimakopoulou 11-Jul-24 11:24am    
Thank you so much, I didn't know that StreamWriter is a polymorphism function.
You are right for the function too. Because I only update the LastDateUsed.
Thank you so much.

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