关闭

.NET-File, XmlDocument, WebService读写文件APIs总结

标签: netweb servicexmlfile
343人阅读 评论(0) 收藏 举报
分类:

File读文件:

            var path = Path.Combine(Environment.CurrentDirectory, "App.config");
            if (!File.Exists(path))
                throw new FileNotFoundException("本地App.config文件不存在!");
            byte[] byteArray = File.ReadAllBytes(path); //读路径中的文件得到字节数组

File写文件:

           var path = Path.Combine(Environment.CurrentDirectory, "App.config");
            //删除原有模板
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            if (bytes != null)
                File.WriteAllBytes(localAppConfigPath, bytes);

XmlDocument读xml文件:

            var doc = new XmlDocument();
            var path = Path.Combine(Environment.CurrentDirectory, "App.config");
            if (!File.Exists(path))
                throw new FileNotFoundException("本地app.config文件不存在!");
            doc.Load(path);
            byte[] bytes = Encoding.ASCII.GetBytes(doc.InnerXml);

XmlDocument写文件:

            doc.RemoveAll();
            doc.InnerXml = docServer.InnerXml;
            doc.Save(path);

WebService接口
读文件接口:

        [WebMethod]
        public byte[] ReadFromFile(string fileName)
        {
            try
            {
                fileName = Path.Combine("ServerFiles", fileName);
                fileName = Path.Combine(Server.MapPath("."), fileName);
                if (File.Exists(fileName))
                {
                    FileStream stream = File.Open(fileName, FileMode.Open);
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, ((int)stream.Length));
                    stream.Close();
                    return buffer;
                }
                throw new Exception("服务器上的文件不存在!");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

写文件接口:

        [WebMethod]
        public bool WriteToFile(string fileName, byte[] buffer)
        {
            try
            {
                fileName = Path.Combine("ServerFiles", fileName);
                fileName = Path.Combine(Server.MapPath("."), fileName);
                FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                stream.Write(buffer, 0, buffer.Length);
                stream.Close();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
2
0

猜你在找
深度学习基础与TensorFlow实践
【在线峰会】前端开发重点难点技术剖析与创新实践
【在线峰会】一天掌握物联网全栈开发之道
【在线峰会】如何高质高效的进行Android技术开发
机器学习40天精英计划
Python数据挖掘与分析速成班
微信小程序开发实战
JFinal极速开发企业实战
备战2017软考 系统集成项目管理工程师 学习套餐
Python大型网络爬虫项目开发实战(全套)
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    Leetcode Solutions in Github

    Click me

    Leetcode solutions in Github, including tags: Array, HashTable, Stack, Queue, LinkedList, Tree, Graph,...

    leetcode-manager

    Click me

    Current Leetcode manager: it's used by Entity Framework, WinForms, Sql Server to manage leetcode questions that we have done. Functions include tags creation, revise and deletion, and questions creation, edit and deletion.

    个人资料
    • 访问:67776次
    • 积分:3612
    • 等级:
    • 排名:第8611名
    • 原创:232篇
    • 转载:0篇
    • 译文:0篇
    • 评论:46条
    博客专栏