Jump to content

C# Create a multi line text file

AlTech
Go to solution Solved by bughtoo,
Just now, AluminiumTech said:

Could you give an example of how I might make folder in the AppData folder?

i edited it here again :  http://stackoverflow.com/questions/16500080/how-to-create-appdata-folder-with-c-sharp

Hi all,

 

I'm trying to experiment with a new Checking for update system and to do this I need to be able to create a textfile with some information in it everytime the application loads up.

 

I tried using this -> https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

 

This is the actual code.

     string[] lines =
            {
                "Version " + Settings.App.Default.SpecsBossVersionTag,
                "Genuine_AluminiumTech",
                "Official_Release"
            };          

		string path = Environment.SpecialFolder.ApplicationData.ToString() + "\\AluminiumTech\\SpecsBoss.txt";

            if (Directory.Exists(path))
            {
                Directory.Delete(path);

                //Write the version to the text file so that our updater can work properly.
                File.WriteAllLines(Environment.SpecialFolder.ApplicationData.ToString() + "\\AluminiumTech\\SpecsBoss.txt", lines);
            }
            else if (!Directory.Exists(path))
            {
                //Write the version to the text file so that our updater can work properly.
                File.WriteAllLines(Environment.SpecialFolder.ApplicationData.ToString() + "\\AluminiumTech\\SpecsBoss.txt", lines);
            }

 

I basically want to have a custom folder in the ApplicationData folder for users and ensure that a text file with the info above.

 

This is the error I have been getting.

error.PNG

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, AluminiumTech said:

Snip

As it says, the directory you are trying to open is not there. First you have to check if its there and then create it if its not there :)

ASUS Maximus VII Hero | i7 4790K OC 4.8GHZ | 4x8GB 2400MHz MSI GTX 1070 alphacool eiswolf gpx pro
Samsung 850 EVO 520GB + Corsair 525gb + 275gb SSD  2TB Seagate Barracuda | 8TB Seagate Archive

Cooler Master HAF-X 942 | EVGA Supernova 1000W 80+ Platinum | Custom watercooling loop (gpu+cpu)

360mm+180mm rad and 10 fans | Swiftech D5 mcp655-B

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, bughtoo said:

As it says, the directory you are trying to open is not there. First you have to check if its there and then create it if its not there :)

I tried to do that but it wasn't making the folder in the AppData folder.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, AluminiumTech said:

I tried to do that but it wasn't making the folder in the AppData folder.

AppData require administrator rights if i am not mistaken, maybe that might your problem. 

EDIT:

NWM THIS IS WRONG :) 

does this not work? http://stackoverflow.com/questions/16500080/how-to-create-appdata-folder-with-c-sharp

ASUS Maximus VII Hero | i7 4790K OC 4.8GHZ | 4x8GB 2400MHz MSI GTX 1070 alphacool eiswolf gpx pro
Samsung 850 EVO 520GB + Corsair 525gb + 275gb SSD  2TB Seagate Barracuda | 8TB Seagate Archive

Cooler Master HAF-X 942 | EVGA Supernova 1000W 80+ Platinum | Custom watercooling loop (gpu+cpu)

360mm+180mm rad and 10 fans | Swiftech D5 mcp655-B

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, bughtoo said:

AppData require administrator rights if i am not mistaken, maybe that might your problem. 

EDIT:

NWM THIS IS WRONG :) 

Could you give an example of how I might make folder in the AppData folder?

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, AluminiumTech said:

Could you give an example of how I might make folder in the AppData folder?

i edited it here again :  http://stackoverflow.com/questions/16500080/how-to-create-appdata-folder-with-c-sharp

ASUS Maximus VII Hero | i7 4790K OC 4.8GHZ | 4x8GB 2400MHz MSI GTX 1070 alphacool eiswolf gpx pro
Samsung 850 EVO 520GB + Corsair 525gb + 275gb SSD  2TB Seagate Barracuda | 8TB Seagate Archive

Cooler Master HAF-X 942 | EVGA Supernova 1000W 80+ Platinum | Custom watercooling loop (gpu+cpu)

360mm+180mm rad and 10 fans | Swiftech D5 mcp655-B

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, M.Yurizaki said:

AppData is user owned. It's how Chrome gets away with installing itself without invoking UAC.

 

I think the problem is you need to actually create the directory before writing files to it.

 

16 minutes ago, bughtoo said:

AppData require administrator rights if i am not mistaken, maybe that might your problem. 

EDIT:

NWM THIS IS WRONG :) 

does this not work? http://stackoverflow.com/questions/16500080/how-to-create-appdata-folder-with-c-sharp

Thanks guys.

 

This code ended up working:

 

      // The folder for the roaming current user 
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            // Combine the base folder with your specific folder....
            string specificFolder = Path.Combine(folder, "AluminiumTech");

            string moreSpecficFolder = Path.Combine(specificFolder, "Specs Boss");

            string path = moreSpecficFolder + "\\SpecsBoss_Version.txt";

            if (Directory.Exists(path))
            {
                Directory.Delete(path);

                Directory.CreateDirectory(moreSpecficFolder);

                //Write the version to the text file so that our updater can work properly.
                File.WriteAllLines(path, lines);
            }
            else if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(moreSpecficFolder);

                //Write the version to the text file so that our updater can work properly.
                File.WriteAllLines(path, lines);
            }

 

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, AluminiumTech said:

snip

 

Seems ok but why would you delete the folder and create it again if it already exists? 

ASUS Maximus VII Hero | i7 4790K OC 4.8GHZ | 4x8GB 2400MHz MSI GTX 1070 alphacool eiswolf gpx pro
Samsung 850 EVO 520GB + Corsair 525gb + 275gb SSD  2TB Seagate Barracuda | 8TB Seagate Archive

Cooler Master HAF-X 942 | EVGA Supernova 1000W 80+ Platinum | Custom watercooling loop (gpu+cpu)

360mm+180mm rad and 10 fans | Swiftech D5 mcp655-B

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, AluminiumTech said:

string path = moreSpecficFolder + "\\SpecsBoss_Version.txt"; 
if (Directory.Exists(path))

 

Isn't that will always return false since you're checking if directory exists on file path?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, bughtoo said:

Seems ok but why would you delete the folder and create it again if it already exists? 

So I can overwrite the file and always have the correct information from the file.


I expect the contents of the file to be updated after users install the app. Thus I need to be able to delete it and re-make it to ensure the contents is correct

 

1 minute ago, Mr_KoKa said:

Isn't that will always return false since you're checking if directory exists on file path?

This will return true if the user runs the program after the initial setup and opening of the program.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, AluminiumTech said:

snip

Well to delete one file you dont have to delete the whole folder :D And there are i think parameters of the functions you are using which doesnt Append to file but Rewrite it :)

ASUS Maximus VII Hero | i7 4790K OC 4.8GHZ | 4x8GB 2400MHz MSI GTX 1070 alphacool eiswolf gpx pro
Samsung 850 EVO 520GB + Corsair 525gb + 275gb SSD  2TB Seagate Barracuda | 8TB Seagate Archive

Cooler Master HAF-X 942 | EVGA Supernova 1000W 80+ Platinum | Custom watercooling loop (gpu+cpu)

360mm+180mm rad and 10 fans | Swiftech D5 mcp655-B

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, AluminiumTech said:

This will return true if the user runs the program after the initial setup and opening of the program.

But you're checking Directory.Exists on a file, which isn't directory, even if the file will exists, Directory.Exist will return false for file.

var filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Advanced\settings.ini";

            Console.WriteLine("Directory.Exists: {0}", Directory.Exists(filePath));
            Console.WriteLine("File.Exists: {0}", File.Exists(filePath));

The above code would return

Directory.Exists: False
File.Exists: True

 

For existing file.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mr_KoKa said:

But you're checking Directory.Exists on a file, which isn't directory, even if the file will exists, Directory.Exist will return false for file.


var filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Advanced\settings.ini";

            Console.WriteLine("Directory.Exists: {0}", Directory.Exists(filePath));
            Console.WriteLine("File.Exists: {0}", File.Exists(filePath));

The above code would return


Directory.Exists: False
File.Exists: True

 

For existing file.

K.

 

I'll just make it delete the directory and replace it every time.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, AluminiumTech said:

K.

 

I'll just make it delete the directory and replace it every time.

WriteAllLines documentation says that "If the target file already exists, it is overwritten." so there is no need to delete directory.

 

Also, this file is created for another application right? Cause you only create file, and never read it in this code.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mr_KoKa said:

WriteAllLines documentation says that "If the target file already exists, it is overwritten." so there is no need to delete directory.

 

Also, this file is created for another application right? Cause you only create file, and never read it in this code.

The file is used in another program.

 

I will remove the bit forcing it to delete the directory every time.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×