public static void Serialize <T>(string jsonFilePath, T obj, bool overwrite = true) { using (var fileStream = FileStreamHelper.NewWrite(jsonFilePath, overwrite)) { JsonStreamSerializer.Serialize(fileStream, obj); } }
public static void Serialize <T>(string filePath, T value, IStreamSerializer <T> streamSerializer, bool overwrite = true) { using (var fileStream = FileStreamHelper.NewWrite(filePath, overwrite)) { streamSerializer.Serialize(fileStream, value); } }
private async Task DeserializeExampleProjectFile() { var exampleVisualStudioProjectFilePath01 = this.TestingDataDirectoryContentPathsProvider.GetExampleVisualStudioProjectFilePath01(); var projectFile = await this.VisualStudioProjectFileSerializer.DeserializeAsync(exampleVisualStudioProjectFilePath01); var testingDataDirectoryPathProvider = this.ServiceProvider.GetRequiredService <ITestingDataDirectoryPathProvider>(); var testingDataDirectoryPath = testingDataDirectoryPathProvider.GetTestingDataDirectoryPath(); var stringlyTypedPathOperator = this.ServiceProvider.GetRequiredService <IStringlyTypedPathOperator>(); // Pretend to serialize to a file in the testing data directory to get the project reference relative file paths to be correct. var tempOutputFilePath01 = stringlyTypedPathOperator.GetFilePath(testingDataDirectoryPath, "ProjectFile01.csproj"); var visualStudioProjectFileStreamSerializer = this.ServiceProvider.GetRequiredService <IVisualStudioProjectFileStreamSerializer>(); using (var memoryStream = new MemoryStream()) { await visualStudioProjectFileStreamSerializer.SerializeAsync(memoryStream, tempOutputFilePath01, projectFile); // Now actually serialize to the output file. var outputFilePath01 = this.TemporaryDirectoryFilePathProvider.GetTemporaryDirectoryFilePath("ProjectFile01.csproj"); using (var fileStream = FileStreamHelper.NewWrite(outputFilePath01)) { fileStream.Write(memoryStream.ToArray()); } } }
public void Serialize(string filePath, T value, bool overwrite = true) { using (var fileStream = FileStreamHelper.NewWrite(filePath, overwrite)) { this.StreamSerializer.Serialize(fileStream, value); } }
/// <summary> /// 下载项目资源 /// </summary> /// <param name="id"></param> /// <param name="projectId"></param> /// <param name="operateType"></param> /// <returns></returns> public ActionResult DownLoadResource(long id, long projectId, string operateType) { var resource = ProjectService.GetResourceById(id); if (resource == null) { return (Content( "<script >alert('对不起,该文件已被删除,无法下载!');self.location='/Project/SummaryData?projectId=" + projectId + "&operateType=" + operateType + "'</script >", "text/html")); } var sourceUrl = FileStreamHelper.GetPathAtApplication(resource.FilePath); byte[] data; try { var fs = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read); data = new byte[(int)fs.Length]; fs.Read(data, 0, data.Length); fs.Close(); } catch { return (Content( "<script >alert('对不起,该文件已被删除,无法下载!');self.location='/Project/SummaryData?projectId=" + projectId + "&operateType=" + operateType + "'</script >", "text/html")); } return(File(data, "application/octet-stream", resource.FileName)); }
public static void Serialize(string projectFilePath, ProjectFileModel projectFile, bool overwrite = true) { using (var fileStream = FileStreamHelper.NewWrite(projectFilePath, overwrite)) { projectFile.XmlDocument.Save(fileStream); } }
public static void Serialize(string projectFilePath, ProjectFileModel projectFile, bool overwrite = true) { using (var fileStream = FileStreamHelper.NewWrite(projectFilePath, overwrite)) using (var xmlWriter = XmlWriterHelper.New(fileStream)) { projectFile.ProjectElement.Value.Save(xmlWriter); } }
public static T Deserialize <T>(string filePath, IStreamSerializer <T> streamSerializer) { using (var fileStream = FileStreamHelper.NewRead(filePath)) { var value = streamSerializer.Deserialize(fileStream); return(value); } }
public static T Deserialize <T>(string binaryFilePath) { using (var fileStream = FileStreamHelper.NewRead(binaryFilePath)) { var output = BinaryStreamSerializer.Deserialize <T>(fileStream); return(output); } }
public T Deserialize(string filePath) { using (var fileStream = FileStreamHelper.NewRead(filePath)) { var value = this.StreamSerializer.Deserialize(fileStream); return(value); } }
public static T Deserialize <T>(string jsonFilePath) { using (var fileStream = FileStreamHelper.NewRead(jsonFilePath)) { var output = JsonStreamSerializer.Deserialize <T>(fileStream); return(output); } }
/// <summary> /// 写入日志到本地硬盘 /// </summary> /// <param name="concent">记录内容</param> /// <param name="userAccount">操作用户</param> /// <param name="logType">日志类型</param> public void Ilog(string concent, string userAccount = "", LogTypeEnum logType = LogTypeEnum.Info) { string token = DateTime.Now.ToString("yyyyMMddHH"); string logRoot = FileStreamHelper.GetPathAtApplication("log"); StreamWriter sw = new StreamWriter(logRoot, true); sw.WriteLine(concent); sw.Close();//写入 }
public async Task <ProjectFile> DeserializeAsync(string projectFilePath) { using (var fileStream = FileStreamHelper.NewRead(projectFilePath)) { var projectFile = await this.VisualStudioProjectFileStreamSerializer.DeserializeAsync(fileStream, projectFilePath); return(projectFile); } }
public void ReadContent_UnfinishedLine() { FileStreamHelper f = new FileStreamHelper(); f.WriteReadCheck("hello world\r", String.Empty); f.WriteReadCheck("hello world\r\n", "hello world\r\n"); f.WriteReadCheck("", String.Empty); f.WriteReadCheck(" ", String.Empty); f.WriteReadCheck("", String.Empty); }
private static void EnsureBOMProduced() { var tempFilePath = @"C:\Temp\temp.txt"; using (var fileStream = FileStreamHelper.NewWrite(tempFilePath)) using (var writer = StreamWriterHelper.NewLeaveOpenAddBOM(fileStream)) { writer.WriteLine("Hello world! (BOM?)"); } }
public static void Serialize(string solutionFilePath, SolutionFile obj, bool overwrite = true) { using (var fileStream = FileStreamHelper.NewWrite(solutionFilePath, overwrite)) using (var textWriter = StreamWriterHelper.NewLeaveOpenAddBOM(fileStream)) { var solutionFileTextSerialializer = new SolutionFileTextSerializer(); solutionFileTextSerialializer.Serialize(textWriter, obj); } }
public static SolutionFile Deserialize(string solutionFilePath) { using (var fileStream = FileStreamHelper.NewRead(solutionFilePath)) using (var textReader = StreamReaderHelper.NewLeaveOpen(fileStream)) { var solutionFileTextSerialializer = new SolutionFileTextSerializer(); var solutionFile = solutionFileTextSerialializer.Deserialize(textReader); return(solutionFile); } }
private void btnEdit_Click(object sender, EventArgs e) { TreeNode node = DataTree.SelectedNode; if (node != null && !string.IsNullOrEmpty(ReplaceName)) { if (ReplaceName == node.Text) { XmlNodeList nodelist = configele.SelectNodes(string.Format("Item[@Name='{0}']", ReplaceName)); if (nodelist.Count > 0) { //保存XML XmlElement ele = (XmlElement)nodelist[0]; ele.SetAttribute("Type", cbbConfigType.Text); ele.SetAttribute("DataBase", cbbDataBase.Text); ele.SetAttribute("Path", tbxPath.Text); oper.Save(); //保存文件 FileInfo fi = new FileInfo(string.Format("{0}{1}{2}\\{3}", Application.StartupPath, configDataPath, configItemName, ReplaceName)); if (fi.Exists) { fi.Delete(); } FileStreamHelper.SaveText(fi.DirectoryName, ReplaceName, Content); } } else if (configele.SelectNodes(string.Format("Item[@Name='{0}']", ReplaceName)).Count > 0) { MessageBox.Show(string.Format("已存在名称为({0})的明细", ReplaceName)); } else { XmlNodeList nodelist = configele.SelectNodes(string.Format("Item[@Name='{0}']", node.Text)); if (nodelist.Count > 0) { //保存XML XmlElement ele = (XmlElement)nodelist[0]; ele.SetAttribute("Name", ReplaceName); ele.SetAttribute("Type", cbbConfigType.Text); ele.SetAttribute("DataBase", cbbDataBase.Text); ele.SetAttribute("Path", tbxPath.Text); oper.Save(); //保存文件 FileInfo fi = new FileInfo(string.Format("{0}{1}{2}\\{3}", Application.StartupPath, configDataPath, configItemName, node.Text)); if (fi.Exists) { fi.Delete(); } FileStreamHelper.SaveText(fi.DirectoryName, ReplaceName, Content); RefreshList(); } } } }
public void ReadContent_BigContent() { FileStreamHelper f = new FileStreamHelper(); var fileStream = f.Object(); FileReadOnlyWrapper w = new FileReadOnlyWrapper("dummy.txt", fileStream); string bigInput = new String((char)65, 65536); bigInput += Environment.NewLine; f.WriteReadCheck(bigInput, bigInput, bigInput.Length); }
public async Task <XDocumentVisualStudioProjectFile> DeserializeAsync(string projectFilePath) { using (var fileStream = FileStreamHelper.NewRead(projectFilePath)) { // Deserialize. var xElementVisualStudioProjectFile = await this.RelativePathsXElementVisualStudioProjectFileStreamSerializer.DeserializeAsync(fileStream, this.MessageSink); // Modify. var modifiedXElementVisualStudioProjectFile = await this.FunctionalVisualStudioProjectFileSerializationModifier.ModifyDeserializeationAsync(xElementVisualStudioProjectFile, projectFilePath, this.MessageSink); return(modifiedXElementVisualStudioProjectFile); } }
public void ReadContent_BasicReads_Umlaute() { FileStreamHelper f = new FileStreamHelper(); var fileStream = f.Object(); FileReadOnlyWrapper w = new FileReadOnlyWrapper("dummy.txt", fileStream); string input = "hello worldöäüÖÄÜß你好,世界\r\n"; f.WriteReadCheck(input, input); f.WriteReadCheck("\r\n", "\r\n"); f.WriteReadCheck("\n", "\n"); }
public async Task SerializeAsync(string actualfilePath, string asFilePath, XDocumentVisualStudioProjectFile xElementVisualStudioProjectFile, bool overwrite = true) { // Modify. var modifiedXElementVisualStudioProjectFile = await this.FunctionalVisualStudioProjectFileSerializationModifier.ModifySerializationAsync(xElementVisualStudioProjectFile, asFilePath, this.MessageSink); // Prettify. await this.VisualStudioProjectFileXDocumentPrettifier.Prettify(modifiedXElementVisualStudioProjectFile.VisualStudoProjectFileXDocument); // Serialize. using (var fileStream = FileStreamHelper.NewWrite(actualfilePath, overwrite)) { await this.RelativePathsXDocumentVisualStudioProjectFileStreamSerializer.SerializeAsync(fileStream, modifiedXElementVisualStudioProjectFile, this.MessageSink); } }
public void ReadContent_Read2Lines() { FileStreamHelper f = new FileStreamHelper(); var fileStream = f.Object(); FileReadOnlyWrapper w = new FileReadOnlyWrapper("dummy.txt", fileStream); string returnStringX = "h1\r\n"; string input = "\r\nh1\r\nhello world\r\n"; f.WriteReadCheck(input, "\r\n"); input = "h1\r\nhello world"; f.WriteReadCheck(input, returnStringX); }
private void SelectSingleItem(string itemName) { tbxName.Text = itemName; XmlNodeList nodelist = configele.SelectNodes(string.Format("Item[@Name='{0}']", itemName)); if (nodelist.Count > 0) { XmlElement ele = (XmlElement)nodelist[0]; tbxName.Text = ele.GetAttribute("Name"); cbbConfigType.Text = ele.GetAttribute("Type"); cbbDataBase.Text = ele.GetAttribute("DataBase"); tbxPath.Text = ele.GetAttribute("Path"); tbxContent.Text = FileStreamHelper.ReadText(string.Format("{0}{1}{2}\\{3}", Application.StartupPath, configDataPath, configItemName, ele.GetAttribute("Name"))); } }
private async Task TestPrettificationOfProjectXElement() { var projectXElementPrettifier = this.ServiceProvider.GetRequiredService <IVisualStudioProjectFileXDocumentPrettifier>(); var inputProjectFilePath = this.TestingDataDirectoryContentPathsProvider.GetExampleVisualStudioProjectFilePath01(); var relativeFilePathsVisualStudioProjectFileStreamSerializer = this.ServiceProvider.GetRequiredService <IRelativePathsXDocumentVisualStudioProjectFileStreamSerializer>(); using (var inputFileStream = FileStreamHelper.NewRead(inputProjectFilePath)) { var visualStudioProjectFile = await relativeFilePathsVisualStudioProjectFileStreamSerializer.DeserializeAsync(inputFileStream, this.MessageSink); var xElementVisualStudioProjectFile = visualStudioProjectFile as XDocumentVisualStudioProjectFile; await projectXElementPrettifier.Prettify(xElementVisualStudioProjectFile.VisualStudoProjectFileXDocument); } }
private async Task CompareRoundTripBasicXmlSerializedFiles() { var inputProjectFilePath = this.TestingDataDirectoryContentPathsProvider.GetExampleVisualStudioProjectFilePath01(); var outputProjectFilePath = this.TemporaryDirectoryFilePathProvider.GetTemporaryDirectoryFilePath("ProjectFile01.csproj"); //var xElement = XElement.Load(inputProjectFilePath); // Does not preserve insignifcant whitespace. var xElement = XElement.Load(inputProjectFilePath, LoadOptions.PreserveWhitespace); // Because insignification whitespace is preserved, project file is identical when round-tripped. //xElement.Save(outputProjectFilePath); // Adds an XML document declaration, so no good a a project file serializer. using (var fileStream = FileStreamHelper.NewWrite(outputProjectFilePath)) using (var xmlWriter = XmlWriterHelper.New(fileStream)) { xElement.Save(xmlWriter); } await this.CompareFiles(inputProjectFilePath, outputProjectFilePath, "Files not equal."); }
private async Task CompareRoundTripRelativeFilePathsSerializedFiles() { var inputProjectFilePath = this.TestingDataDirectoryContentPathsProvider.GetExampleVisualStudioProjectFilePath01(); var outputProjectFilePath = this.TemporaryDirectoryFilePathProvider.GetTemporaryDirectoryFilePath("ProjectFile01.csproj"); var relativeFilePathsVisualStudioProjectFileStreamSerializer = this.ServiceProvider.GetRequiredService <IRelativePathsXDocumentVisualStudioProjectFileStreamSerializer>(); using (var inputFileStream = FileStreamHelper.NewRead(inputProjectFilePath)) { var visualStudioProjectFile = await relativeFilePathsVisualStudioProjectFileStreamSerializer.DeserializeAsync(inputFileStream, this.MessageSink); using (var outputFileStream = FileStreamHelper.NewWrite(outputProjectFilePath)) { await relativeFilePathsVisualStudioProjectFileStreamSerializer.SerializeAsync(outputFileStream, visualStudioProjectFile, this.MessageSink); } } await this.CompareFiles(inputProjectFilePath, outputProjectFilePath, "Files not equal."); }
/// <summary> /// 回调函数 /// </summary> /// <param name="msg"></param> private void Callback(string msg) { if (msg.StartsWith("length")) { domainlength = int.Parse(msg.Substring(6)); } else { if (!msg.Contains("不可用")) { var d = msg.Substring(0, msg.Length - 2); result.Add(d); this.listBox1.Items.Add(d); if (result.Count % 100 == 0) { FileStreamHelper.SaveText(currentpath, string.Format("{0}.txt", DateTime.Now.ToString("yyyyMMddhhmmss")), string.Join(@" ", result)); result.Clear(); } } //未完成 if (searchindex + 1 < domainlength) { toolStripProgressBar1.Value = domainlength > 0 ? (searchindex + 1) * toolStripProgressBar1.Maximum / domainlength : toolStripProgressBar1.Maximum; toolStripStatusLabel1.Text = string.Format("({1}/{2}){0}", msg, searchindex + 1, domainlength); } //已完成 else { toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; toolStripStatusLabel1.Text = string.Format("({1}/{2}){0} 全部完成", msg, searchindex + 1, domainlength); if (result.Count > 0) { FileStreamHelper.SaveText(currentpath, string.Format("{0}.txt", DateTime.Now.ToString("yyyyMMddhhmmss")), string.Join(@" ", result)); result.Clear(); } } searchindex++; } }
public string SaveTimesheetExpense(int timesheetId, string title, DateTime date, string filePath) { SqlCommand command = GetCommandForTimesheetExpenseUpload(timesheetId, title, date, filePath); var fsh = new FileStreamHelper(); var conn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; fsh.SaveDocumentWithoutFileStream(command, conn, filePath); return command.Parameters["@DocumentId"].Value.ToString(); }
public string SaveInvoiceFile(int invoiceId, string filePath) { SqlCommand command = GetCommandForInvoiceUpload(invoiceId, filePath); var fsh = new FileStreamHelper(); var conn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; fsh.SaveDocumentWithoutFileStream(command, conn, filePath); return command.Parameters["@DocumentId"].Value.ToString(); }
public ActionResult DownloadInvoice(int id) { FileDownloadModel inv = this.associateService.GetInvoiceFile(id); return(FileStreamHelper.FileDownloadModelResult(inv, this.HttpContext, true)); }
public ActionResult DownloadFile(Guid?id) { var inv = this.associateService.GetInvoiceFile(null, id); return(FileStreamHelper.FileDownloadModelResult(inv, this.HttpContext, true)); }