Autor Beitrag
tomo2403
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: Fr 10.08.18 18:57 
Hey Leute, :wave:

hab mal ne Frage. Ich weiß wie man in einer WinForm daten speichern und laden kann.
Kann mir aber bitte mal jemand sagen wie man Ordner erstellt? :gruebel:

So speichere und lade ich:

ausblenden volle Höhe Klasse
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
using System;
using System.Runtime.Serialization.Formatters.Binary;

namespace App
{
    [Serializable]
    public class AppData
    {
        public string data { get; set; }

        public void save(string path)
        {
            System.IO.FileStream FS = new System.IO.FileStream(path, System.IO.FileShare.Create);
            BinaryFormatter BF = new BinaryFormatter();


            BF.Serialize(FS, this);
            FS.Dispose();

        }

        public AppData load(string path)
        {
            AppData AD = new AppData();

            System.IO.FileStream FS = new System.IO.FileStream(path, System.IO.FileMode.Open);
            BinaryFormatter BF = new BinaryFormatter();

            AD = (AppData)BF.Deserialize(FS);

            FS.Dispose();
            return AD;
        }
    }
}

ausblenden Laden
1:
2:
3:
AppData ADl = new AppData();
ADl = ADl.load(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Datei.dll");
variable= ADl.data;

ausblenden Speichern
1:
2:
3:
AppData ADs = new AppData();
ADs.data = "";
ADs.save(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\Datei.dll");


Moderiert von user profile iconTh69: Titel geändert ("stellen" -> "erstellen").
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 10.08.18 19:16 
Ruf einfach
ausblenden C#-Quelltext
1:
Directory.CreateDirectory(path);					

auf.
CreateDirectory erzeugt alle fehlende Ordner die im Pfad stecken. Wenn in "c:/Dir1/Dir2/Dir3" der Ordner dir1 schon existiert werden nur die Unterordner 2 und 3 erstellt.
Heißt dort wo du den Ordner brauchst musst du nicht testen ob der Ordner schon existiert du kannst einfach immer CreateDirectory aufrufen. Im Zweifel wenn der Ordner schon existiert macht die Methode halt einfach nix.

Da du das vermutlich in AppData.save brauchst rufe da am Anfang einmal
ausblenden C#-Quelltext
1:
Directory.CreateDirectory(Path.GetDirectoryName(path));					

aus. Da bei dir scheinbar der path nicht der Ordnername sondern der Filename ist mußt du aus dem Filename per Path.GetDirectoryName den Ordnernamen extrahieren.

Für diesen Beitrag haben gedankt: tomo2403
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: Fr 10.08.18 20:05 
Wenn ich aber
ausblenden C#-Quelltext
1:
Directory.CreateDirectory(Path.GetDirectoryName(Environment.SpecialFolder.ProgramFiles.ToString()) + "\\dir1\\dir2");					

oder
ausblenden C#-Quelltext
1:
Directory.CreateDirectory((Environment.SpecialFolder.ProgramFiles.ToString()) + "\\dir1\\dir2");					

anwende, passiert nichts.

Immer wenn ich auf Systemordner zugreifen möchte wir dies verweigert!
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 10.08.18 20:24 
In ProgramFiles hat ein normaler User auch keine Rechte. Da gehören nur Programdateien hin die von z.B. einem Installer dahingeschoben werden.
Wenn du irgendwas zur Laufzeit irgendwo ablegen musst dann gehört das eher in einen der ApplicationData Ordner. Du nennst deine Klasse ja auch schon AppData ;)
Welchen ApplicationData Ordner genau hängt dann davon ab was du willst. Per User, für alle User gleich, soll das mit dem Profil auf andere Rechner roamen etc.


Wenn du trotzdem aus irgendeinem Grund, gegen die Best Practices von Windows, doch da was hioschreiben willst muss du dir für dein Programm oder zumindest diesen Codeteil erhöhte Rechte anfordern. Stichwort User Access Control (UAC).
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: Fr 10.08.18 20:37 
ApplicationData funktioniert auch nicht und mit UAC komme ich nicht klar. :(
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 10.08.18 20:45 
Dann sollten wir uns mal fragen wie du das ausprobierst. Wenn das ein Rechteproblem ist oder sonst aus irgend einem Grund der Ordner nicht angelegt werden konnte bekommst du eine Exception und die sollte dir sagen was das Problem ist. Mindestens beim debuggen sollte du die prominent zu sehen bekommen.
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: Fr 10.08.18 20:52 
Fehler im Bild

(Statt Desktop steht da eigentlich AppData in der Fehlermeldung)
Einloggen, um Attachments anzusehen!
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 10.08.18 21:16 
Ich vermisse in der Methode irgendwie den besprochenen Aufruf von Directory.CreateDirectory :gruebel:
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: Sa 11.08.18 16:33 
Es geht immer noch nicht! Kannst du mir bitte ein Codebeispiel erstellen? Ich bin einfach zu blöd. :?

Außerdem wir die Methode ... nicht aufgerufen!
ausblenden C#-Quelltext
1:
2:
Directory.CreateDirectory((Environment.SpecialFolder.ApplicationData.ToString() + "\\T-App Germany\\Knuffi, der programmierbare Hund\\"));
Directory.CreateDirectory(Path.GetDirectoryName(Environment.SpecialFolder.ApplicationData.ToString() + "\\T-App Germany\\Knuffi, der programmierbare Hund\\"));


Könnte es vielleicht daran liegen, dass der Ordner AppData versteckt ist?
Einloggen, um Attachments anzusehen!
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Sa 11.08.18 18:02 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
public void save(string path)
{
    Directory.CreateDirectory(Path.GetDirectoryName(path)); // <- einfach einfügen
    System.IO.FileStream FS = new System.IO.FileStream(path, System.IO.FileShare.Create);
    BinaryFormatter BF = new BinaryFormatter();

    BF.Serialize(FS, this);
    FS.Dispose();
}



Zitat:
Könnte es vielleicht daran liegen, dass der Ordner AppData versteckt ist?


Nein, versteckt heißt einfach nur da du den in verschiedenen Tools wie zum Beispiel den Explorer nicht einfach so siehst. Ändert aber nichts an Rechten.

Für diesen Beitrag haben gedankt: tomo2403
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 11.08.18 18:11 
- Nachträglich durch die Entwickler-Ecke gelöscht -
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: So 12.08.18 12:18 
Ich habe das ganze jetzt einfach so gelöst:
ausblenden Ausschnitt aus Serializable Class
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
        string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "\\T-App Germany\\Knuffi, der programmierbare Hund");

        public void Save(string path)
        {

            Directory.CreateDirectory(folder);
            Directory.CreateDirectory(Path.GetDirectoryName(folder));

            FileStream FS = new FileStream(path, FileMode.Create);
            BinaryFormatter BF = new BinaryFormatter();


            BF.Serialize(FS, this);
            FS.Dispose();
        }


Danke! :) :wink:
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 12.08.18 13:03 
Aber warum 2x CreateDirectory?
Und warum benutzt du nicht den übergebenen Pfad path (so wie Ralf es ja schon geschrieben hat)?

Ich habe den Eindruck, da fehlt ein bißchen das Verständnis für den Code - und "Trial and Error" führt selten zum Erfolg.
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: So 12.08.18 13:41 
Zitat:
Und warum benutzt du nicht den übergebenen Pfad path (so wie Ralf es ja schon geschrieben hat)?

Weil der Code nicht Funktioniert!
Wenn ich das ausführe, kommt wieder der Fehler mit der Zugriffsberechtigung!
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 12.08.18 13:52 
Welchen Pfad übergibst du denn?
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: So 12.08.18 13:56 
ausblenden C#-Quelltext
1:
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "\\T-App Germany\\Knuffi, der programmierbare Hund"					

Das Problem ist das VS nur den Pfad \\T-App Germany\\Knuffi, der programmierbare Hund übergiebt.
ProgramFiles wir nicht mit eingebunden.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 12.08.18 14:17 
Auch das hat Ralf doch schon geschrieben, daß du im ProgramFiles-Ordner (und deren Unterordner) keine Schreibrechte hast (nur System, Admin, Installer, etc.).
Und da dies der selbe Pfad wie dein string folder ist, kann auch der vorherige Code nicht damit funktionieren.
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: So 12.08.18 14:19 
Bei AppData funktioniert es auch nicht!
Er versucht außerdem das ganze unter D:\ zu speichern. (es soll aber C:\)


Zuletzt bearbeitet von tomo2403 am So 12.08.18 14:21, insgesamt 1-mal bearbeitet
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 12.08.18 14:19 
Wie sieht dein kompletter Aufruf der Save-Methode aus?

Btw: Eigenschaften- und Methodennamen sollten mit einem Großbuchstaben beginnen (um sie von privaten Membern zu unterscheiden)
tomo2403 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 46

Win10
C#, VS 2019
BeitragVerfasst: So 12.08.18 14:25 
ausblenden volle Höhe Klasse
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
    [Serializable]
    public class AppData
    {
        public string AdminToolData { get; set; }

        string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\\T-App Germany\\Knuffi, der programmierbare Hund");

        public void Save(string path)
        {
            Directory.CreateDirectory(folder);
            Directory.CreateDirectory(Path.GetDirectoryName(folder));

            FileStream FS = new FileStream(path, FileMode.Create);
            BinaryFormatter BF = new BinaryFormatter();


            BF.Serialize(FS, this);
            FS.Dispose();
        }

        public AppData Load(string path)
        {
            AppData AD = new AppData();

            FileStream FS = new FileStream(path, FileMode.Open);
            BinaryFormatter BF = new BinaryFormatter();

            AD = (AppData)BF.Deserialize(FS);

            FS.Dispose();
            return AD;
        }
    }


ausblenden Speichern/Laden
1:
2:
3:
4:
5:
6:
                AppData ADs = new AppData();
                ADs.AdminToolData = (string)"hier steht text";
                ADs.Save(folder + "\\AppData.dll");

                AppData ADl = new AppData();
                ADl = ADl.Load(folder + "\\AppData.dll");