C# 往磁盘文件写数据(例如txt\xml等)

1.写入方法

/// <summary>
        /// 把字符串写入xml文件
        /// </summary>
        /// <param name="filePath">文件路径 例如:D:\aaa.xml </param>
        /// <param name="value">要写入的值</param>
        public void FileExport(string filePath, string value)
        {
    
    
            if (File.Exists(filePath))
            {
    
    
                File.Delete(filePath);
            }

            FileStream fileStream = null;
            StreamWriter sw = null;
            try
            {
    
    
                fileStream = new System.IO.FileStream(filePath, FileMode.Create);
                sw = new StreamWriter(fileStream, Encoding.UTF8);
                sw.Write(value);
            }
            catch (Exception ex)
            {
    
    
                throw ex;
            }
            finally
            {
    
    
                if (sw != null)
                {
    
    
                    sw.Close();
                    sw.Dispose();
                }

                if (fileStream != null)
                {
    
    
                    fileStream.Dispose();
                }
            }
        }

2.调用示例

FileExport(App.AppPath + "\\WoodFurnReportDB.xml", xml);  //保存到本地

猜你喜欢

转载自blog.csdn.net/weixin_42193688/article/details/105949354