转换前后的文件名相同 [英] same file name before and after conversion

查看:52
本文介绍了转换前后的文件名相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家。

i已经将文件从.txt转换为.xml并将这两个文件保存在同一个文件夹中。我现在面临的唯一问题是我想要两个文件的名称是一样的例如:

在文件夹tom中有txt文件名tom_todaydatetime.txt。在运行我的exe之后,我希望我的xml文件保存,其名称与文本文件的名称相同。怎么可能?

解决方案

最好不要覆盖原始文件,因为你一定会遇到错误并且想要重做一些东西,所以你最好不要创建一个输出文件夹并保存到该文件夹​​中的相同文件名。


是相同的文件名但具有不同的扩展名:





  string  statusFilePath = Directory.GetCurrentDirectory()。ToString()+   \\\\Status \\\\; 
foreach 字符串文件 in Directory.EnumerateFiles(statusFilePath, * .txt))
{
string content = File.ReadAllText(file);
正则表达式= 正则表达式( @ ##(?< parent> \w +)\\\\ n(?< children> [\ w] +)\\\\ n((?< values> [\ S] + )(\\ n(?!#)|


))+,RegexOptions.None);
XElement xeRoot = new XElement( wpassetd ); // root node
foreach (匹配m in expression.Matches(content))
{
XElement xeParent = new XElement(m.Groups [ parent]。Value);

string [] children = m.Groups [ children]。Value.Split(' '); // 将空格用作子项的分隔符
foreach (捕获上限 m.Groups [ ]。捕获)
{
XElement xeChild = new XElement( data); // 将名称child更改为任何合适的
string [] values = cap.Value.Split( new string [] { @ * @},StringSplitOptions.None);
// 检查子项和值计数是否相等
if (children.Length!= values.Length)
throw new 异常( 子项数和值不匹配。);
for int i = 0 ; i < children.Length; i ++)
{
XElement xeChildValue = new XElement(children [i] .ToLower());
XCData cdata = new XCData(values [i]);
xeChildValue.Add(cdata);

xeChild.Add(xeChildValue);
}
xeParent.Add(xeChild);
}
xeRoot.Add(xeParent);
}
XDocument xmldoc = new XDocument();
xmldoc.Add(xeRoot);
xmldoc.Save(statusFilePath1);
// xmldoc.Save(@test.xml);


hello Experts.
i have converted a file from .txt to .xml and kept both the files in same folder successfully.the only problem i am facing now is i want the name of both the files to be same. like for example:
in folder tom there is txt file name tom_todaydatetime.txt. after running my exe i want my xml file to be saved with same name as the name of text file now. how is it possible?

解决方案

It's better not to overwrite the original file since you are bound to get errors and would want to redo things, so you are better off creating a "output" folder and save to the same file name in that folder.


yes same file name but with different extention:


string statusFilePath = Directory.GetCurrentDirectory().ToString() + "\\\\Status\\\\";                    
foreach (string file in Directory.EnumerateFiles(statusFilePath, "*.txt"))
{
string content = File.ReadAllText(file);
 Regex expression = new Regex(@"##(?<parent>\w+)\r\n(?<children>[\w ]+)\r\n((?<values>[\S ]+)(\r\n(?!#)|


))+", RegexOptions.None); XElement xeRoot = new XElement("wpassetd"); //root node foreach (Match m in expression.Matches(content)) { XElement xeParent = new XElement(m.Groups["parent"].Value); string[] children = m.Groups["children"].Value.Split(' '); // Use space as delimter for the children foreach (Capture cap in m.Groups["values"].Captures) { XElement xeChild = new XElement("data"); // Change the name 'child' to whatever suitable string[] values = cap.Value.Split(new string[] { "@*@" }, StringSplitOptions.None); // Check that the children and values counts are equal if (children.Length != values.Length) throw new Exception("The number of children and values mismatch."); for (int i = 0; i < children.Length; i++) { XElement xeChildValue = new XElement(children[i].ToLower()); XCData cdata = new XCData(values[i]); xeChildValue.Add(cdata); xeChild.Add(xeChildValue); } xeParent.Add(xeChild); } xeRoot.Add(xeParent); } XDocument xmldoc = new XDocument(); xmldoc.Add(xeRoot); xmldoc.Save(statusFilePath1); //xmldoc.Save(@"test.xml");


这篇关于转换前后的文件名相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆