コード例 #1
0
        /// <summary>
        /// 处理数据文件并校验数据文件路径
        /// </summary>
        /// <param name="jmxFilePath">jmx文件路径</param>
        /// <returns>处理结果</returns>
        protected bool ProcessDataFilePath(string jmxFilePath)
        {
            string tempFileName = $"{jmxFilePath}.tmp";

            // string fullJmxFilePath = Path.Combine(basicPath, jmxFilePath);
            string          jmxfileName = Path.GetFileName(jmxFilePath);
            string          fileText    = File.ReadAllText(jmxFilePath);
            Regex           regex       = new Regex("<stringProp name=\"filename\">.*</stringProp>", RegexOptions.IgnoreCase);
            MatchCollection matches     = regex.Matches(fileText);

            if (matches.Count > 0)
            {
                for (int i = 0; i < matches.Count; i++)
                {
                    string oldPath = matches[i].Value.Trim(' ');
                    string newPath = oldPath.Replace("<stringProp name=\"filename\">", "").Replace("</stringProp>", "").Trim(' ');
                    if (!string.IsNullOrEmpty(newPath))
                    {
                        string fileName = Path.GetFileName(newPath);
                        if (!string.IsNullOrEmpty(fileName))
                        {
                            string truePath = jmxFilePath.Replace(jmxfileName, fileName);
                            truePath = FileOperaHelper.PathTransformation(truePath);
                            if (!File.Exists(truePath))
                            {
                                throw new FileNotFoundException($"文件不存在或者路径错误或缺少文件,[{truePath}].");
                            }
                            string relativelyPath = FileOperaHelper.PathTransformation(Path.Combine("data", fileName));
                            fileText = fileText.Replace(oldPath, $"<stringProp name=\"filename\">{relativelyPath}</stringProp>");
                        }
                    }
                }
            }

            using (FileStream file = new FileStream(tempFileName, FileMode.OpenOrCreate))
            {
                byte[] bytes = System.Text.Encoding.Default.GetBytes(fileText);
                file.Write(bytes, 0, bytes.Length);
            }
            File.Delete(jmxFilePath);
            FileInfo info = new FileInfo(tempFileName);

            info.MoveTo(tempFileName.Replace(".tmp", string.Empty));
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// 处理上传上来的文件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        protected async Task <bool> DealUploadFile(string id, string fileName)
        {
            return(await Task <bool> .Run(async() =>
            {
                var _extension = Path.GetExtension(fileName).ToLower();
                var fileDirectory = Path.Combine(_jmeterOptions.JmxRootPath, id);
                string sourceFileName = Path.Combine(fileDirectory, fileName);
                switch (_extension)
                {
                case ".zip":
                    //解压文件
                    await FileOperaHelper.ExtractToDirectory(sourceFileName, fileDirectory, true);
                    break;
                }

                await SetStartFile(fileDirectory);
                string targetFileName = Path.Combine(fileDirectory, "start-up.jmx");
                return ProcessDataFilePath(targetFileName);
            }));
        }