简体   繁体   中英

How to save an application's native file to a local folder?

Ok, so I have a gui app that I made in c#, and now I want to save its native file to a disk location when the user clicks Save button and later to open it in the same app when the user clicks Open button or double clicks the file in explorer (same as when we click Save in MS Word for example, and save a Word file and later open it).

I have searched for save application settings and save user settings and I have done this inside my app to save some local variables on some lists, while the app is running.

Can somebody tell me please if there is some function that would collect all the settings that I have in my app and write it to a file in local folder or I have to save all the settings in my app one by one and then somehow write them to a file?

Have your application write all of the information you would like to save to a text file in the application's directory(so the file path will always be the same). You can have a class called SetupSaver or something. You will need a method that checks and sees if a text file exists with a specified name in the applications directory and to create one if not. Then you can just read data from that file as well as write to it.

File Creator method:

public static void logPageCreator()
        {
            if (!System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "logFile.txt"))
            {
                System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "logFile.txt");
            }
            else 
            {
                //File already exists moving on.....
            }
        }

Send data method:

public static void Log(string message)
        {
            try
            {  
                System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "logFile.txt",true);
                file.WriteLine(message);
                file.Close();

                long size = new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "logFile.txt").Length;
                if (size > 5000000)// limits the text file size to this many bytes
                {
                    File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "logFile.txt", String.Empty);
                }
            }
            catch
            {

            }
        }

Data reader method:

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();

Maybe you need the library(or namespace) System.Xml, which is integrated into .netFramework. And here is the link to MSDN which explains the usage of XmlDocument Class: https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=VS.80).aspx

And if you think using the XmlDocuments Class is hard, you can try to write settings into Register.

As for collecting your settings in your app, I would recommend you to create a static class to store the parameters in app.As far as I am concerned, manually collecting the settings in apps is always better than automatically.

I understand you want to save application settings and user settings. This in contrast to a file: one operator may want to create several different files, but his settings remain the same until they are changed, similar like options are changed.

If this is what you are looking for, then the method you can use depends on how many application and user settings you are talking about.

Method 1: Properties.Settings

If you don't have a lot, say less then 30 or something, maybe 50, then the easiest would be to use the application settings of your project.

  • In solution explorer find the project where the application and user setting are used
  • Right click on it and select properties
  • In the new window, on the left you'll see Application / Build / Build Events / etc.
  • Find settings
  • Here you can give the setting a name, a type (string / int / datetime / etc) and a default value.
  • Application settings are usually not changeable, user settings are. Use the scope to select between application and user

Once you've typed all your settings and saved them. The environment has created for you a class that is derived from System.Configuration.ApplicationSettingsBase. This class has properties for all your settings. Access as follows:

Properties.Settings.Default.MySetting = ...
var myOtherSetting = Properties.Settings.Default.MyOtherSetting;

Properties.Settings.Default.Save();

You can Save / Reload / Reset these properties and get notified whenever the properties change. See ApplicationSettingsBase

The user properties are saved per user and automatically loaded when your program starts.

Method 2: Create a settings class

Another possibility is that you create a class with properties for all your settings and load / save them when needed, for instance when the program starts or exist.

Saving them in XML format is easy when you use the XMLFormatter class.

private void SaveAsXML(MySettings settings, string fileName)
{
    using (var streamWriter = File.CreateText(fileName))
    {
        var xmlSerializer = new XMLSerializer(typeof(MySettings));
        xmlSerializer.Serialize(streamWriter, settings);
    }
}

private MySettings ReadAsXML(string fileName)
{
    using (var streamReader = File.OpenText(fileName))
    {
        var xmlSerializer = new XMLSerializer(typeof(MySettings));
        return xmlSerializer.Deserialize(streamReader);
    }
}

Advantage: full control over your settings. You can hand over the settings from one operator to another operator. Disadvantage: you have to take care that they are saved in the proper places

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM