wtt
4 天以前 311ff7d5a864a1e966848412df6d7880804db491
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web.Http;
using WebAPI.Models;
 
namespace WebAPI.Controllers
{
    public class PlateBindingController : ApiController
    {
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
 
        /// <summary>
        /// 保存平板绑定
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [Route("Save/Xt_PlateBinding")]
        [HttpPost]
        public object Save([FromBody] JObject msg)
        {
            try
            {
                DataSet ds;
                var _value = msg["msg"].ToString();
                string msg3 = _value.ToString();
                string[] sArray = msg3.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                string msg1 = sArray[0].ToString();
                string user = sArray[1].ToString();
                string HPath = sArray[2].ToString();
 
                string filePath = "MESConfiguration";
                bool isAppend = true;
 
                filePath = $@"{filePath}\PlateBinding.txt";
 
                //filePath = "C:\\" + filePath;
                filePath = HPath;
 
                if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath)))
                {
                    System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                }
                //判断是否已经存在平板绑定文件,若已存在则删除,重新创建
                bool fileExists = System.IO.File.Exists(filePath);
 
                //存在 则删除该文件
                if (fileExists)
                {
                    File.Delete(filePath);                    
                }
                //创建文件
 
                using (StreamWriter writer = new StreamWriter(filePath, isAppend))
                {
                    //存在的时候才写一行
                    if (fileExists && isAppend)
                    {
                        writer.WriteLine();
                    }
 
                    var content = msg1 is string ? msg1 : JsonConvert.SerializeObject(msg1);
                    writer.WriteLine($"{DateTime.Now}");
                    writer.WriteLine("");
                    msg1 = msg1.Replace("{","").Replace("}","").Replace("\"","");
                    string[] arr = msg1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < arr.Length; i++)
                    {
                        writer.WriteLine(arr[i]);
                    }
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "平板绑定信息写入成功!";
                objJsonResult.data = "";
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "写入平板绑定信息过程中发生异常!";
                objJsonResult.data = "";
                return objJsonResult;
            }
        }
 
        #region 获取平板绑定文件数据
 
        [Route("Get/Xt_PlateBinding")]
        [HttpGet]
        public object Get(string user,string HPath)
        {
            try
            {
                //判断是否存在文件夹,若不存在则创建
                if (!System.IO.Directory.Exists(Path.GetDirectoryName(HPath)))
                {
                    System.IO.Directory.CreateDirectory(Path.GetDirectoryName(HPath));
                }
 
                //判断是否已经存在平板绑定文件,若不存在则创建一份文件
                bool fileExists = System.IO.File.Exists(HPath);                
 
                //不存在则创建该文件
                if (!fileExists)
                {
                    bool isAppend = true;
                    using (StreamWriter writer = new StreamWriter(HPath, isAppend))
                    {
                        //存在的时候才写一行
                        if (fileExists && isAppend)
                        {
                            writer.WriteLine();
                        }
                        string msg1 = "";
                        var content = msg1 is string ? msg1 : JsonConvert.SerializeObject(msg1);
                        writer.WriteLine($"{DateTime.Now}");
                        writer.WriteLine("");
                        msg1 = msg1.Replace("{", "").Replace("}", "").Replace("\"", "");
                        string[] arr = msg1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < arr.Length; i++)
                        {
                            writer.WriteLine(arr[i]);
                        }
                    }
                }
 
                //根据指定路径,读取文件内容,返回数据为数组格式
                string[] ConfigFileInfo = File.ReadAllLines(HPath);
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ConfigFileInfo;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
    }
}