简体   繁体   English

如何将应用程序的本机文件保存到本地文件夹?

[英]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). 好的,所以我有一个用c#制作的gui应用程序,现在我想在用户单击“保存”按钮时将其本机文件保存到磁盘位置,然后在用户单击“打开”按钮或双击时在同一应用程序中将其打开单击资源管理器中的文件(例如,当我们单击“在MS Word中保存”,然后保存Word文件并稍后打开它时)。

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. 您可以拥有一个名为SetupSaver的类。 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. 也许您需要集成到.netFramework中的库(或名称空间)System.Xml。 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 以下是MSDN的链接,该链接解释了XmlDocument类的用法: https ://msdn.microsoft.com/zh-cn/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. 而且,如果您认为使用XmlDocuments类很困难,则可以尝试将设置写入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 方法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. 如果您没有很多东西,比如说少于30个或大约50个,那么最简单的方法就是使用项目的应用程序设置。

  • 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. 在新窗口的左侧,您将看到Application / Build / Build Events /等。
  • 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. 该环境为您创建了一个从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 请参阅ApplicationSettingsBase

The user properties are saved per user and automatically loaded when your program starts. 用户属性将按用户保存,并在程序启动时自动加载。

Method 2: Create a settings class 方法2:创建一个设置类

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. 使用XMLFormatter类时,以XML格式保存它们很容易。

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 缺点:您必须注意将它们保存在正确的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将下载的文件保存到本地文件夹? - How to save downloaded file to local folder? Windows 8-将XAML中的图像另存为本地文件(在Application文件夹中) - Windows 8 - Save Image in XAML as local file ( inside Application folder ) 如何将文件保存在Windows应用程序的文件夹中 - How to save file in a folder in windows application 如何将 aspx 文件另存为 html 文件和文件夹到本地计算机或服务器 - how to save aspx file as html file and folder to local machine or server 如何将文件从服务器文件夹保存到本地驱动器/客户端? - How save file from server folder to local drive/client side? 如何上传文件并将其保存在本地计算机的特定文件夹中? - How to upload a file and save it in an specific folder in local computer? 如何将Report Pdf文件保存到我的本地文件夹 - How to save Report Pdf file to my local folder 将 pdf 文件保存在本地文件夹中或下载 - save an pdf file in local folder or downloads c#如何将IO.Stream保存到MemoryStream并保存到本地文件夹上的PDF文件 - c# how to save IO.Stream to MemoryStream and save to PDF file on local folder 在Silverlight中将png文件保存在应用程序图像文件夹中 - save png file in application image folder in silverlight
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM