Zapis do pliku - File.WriteAllBytes

0

Ma ktoś pomysł czemu plik się nie zapisuje?

using System;
using System.Text;
using System.IO;

namespace Code_test
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = @"F:\Test\save_file.txt";
            string text = "Test_Me.";
            byte[] data = Encoding.ASCII.GetBytes(text);

            File.WriteAllBytes(path, data);
            

            if(!File.Exists(path))
            { Console.WriteLine("File does not exist."); }
            else { Console.WriteLine("The data has been written to the file."); }
            }
            }
            }

Jakoś IF twierdzi że plik się zapisał, ale ja go nie widzę :/

0

U mnie działa. Zmieniłem sobie tylko ścieżkę na @"C:\Temp\save_file.txt"; bo nie mam dysku F:

0

Jakoś IF twierdzi że plik się zapisał, ale ja go nie widzę :/

A dobrze patrzysz?

No i jakaś masakra z klamrami się pod koniec wydarzyła. Wypadałoby też uprościć warunek, bez negacji:

using System;
using System.Text;
using System.IO;

namespace Code_test
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = @"F:\Test\save_file.txt";
            string text = "Test_Me.";
            byte[] data = Encoding.ASCII.GetBytes(text);

            File.WriteAllBytes(path, data);
           
            if (File.Exists(path))
            {
                Console.WriteLine("The data has been written to the file.");                
            }
            else
            {
                Console.WriteLine("File does not exist.");
            }
        }
    }
}

Ale to nie ma wpływu na działanie.

0

Dodałem kod zerżnięty ze stron MS:

using System;
using System.Text;
using System.IO;

namespace Code_test
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = @"G:\Test\savefile.txt";
            string text = "Test_Me.";
            byte[] data = Encoding.ASCII.GetBytes(text);

            File.WriteAllBytes(path, data);
            

            if(File.Exists(path))
            {
                Console.WriteLine("The data has been written to the file.");
            }
            else 
            {
                Console.WriteLine("File does not exist.");
            }

            //----------------------

            string path2 = @"G:\Test\file2.txt";

            try
            {
                // Create the file, or overwrite if the file exists.
                using (FileStream fs = File.Create(path))
                {
                    byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                    // Add some information to the file.
                    fs.Write(info, 0, info.Length);
                }

                // Open the stream and read it back.
                using (StreamReader sr = File.OpenText(path2))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.Read();
        }
    }
}

I nic...

Wyskoczył tylko błąd, może jakaś podpowiedź co się dzieje.sav_to_file.png

0

Teraz 2x zapisujesz do pliku path a potem próbujesz odczytać z path2.

0

Chyba zlokalizowałem problem - wydaje się że czas już na konserwację systemu...
Przegląd katalogu pokazuje 2 pliki, podczas gdy z poziomu WPF (okno SaveFileDialog) pokazuje ich więcej...
Pliki crypto i me to pliki utworzone z poziomu eksploratora Windy
XD
save_the_file.png

1 użytkowników online, w tym zalogowanych: 0, gości: 1