C# 使用if&;时缩短语法;还有其他的声明吗
我目前正在做一个dll库项目C# 使用if&;时缩短语法;还有其他的声明吗,c#,.net,C#,.net,我目前正在做一个dll库项目 if (!Directory.Exists(MenGinPath)) { Directory.CreateDirectory(MenGinPath + @"TimedMessages"); File.WriteAllLines(MenGinPath + @"TimedMessages\timedmessages.txt", new string[] { "Seperate each message with a new line" }); } els
if (!Directory.Exists(MenGinPath))
{
Directory.CreateDirectory(MenGinPath + @"TimedMessages");
File.WriteAllLines(MenGinPath + @"TimedMessages\timedmessages.txt", new string[] { "Seperate each message with a new line" });
}
else if (!File.Exists(MenGinPath + @"TimedMessages\timedmessages.txt"))
{
Directory.CreateDirectory(MenGinPath + @"TimedMessages");
File.WriteAllLines(MenGinPath + @"TimedMessages\timedmessages.txt", new string[] { "Seperate each message with a new line" });
}
如您所见,如果语句Directory.Exists
为false,则将创建特定目录(MenGinPath
)。但是,如果同一路径(另加一个文件)为false,则将调用第二个函数
我的问题是:有没有办法缩短这个时间?
因为正如你所看到的,我调用了两次相同的函数:
Directory.CreateDirectory(MenGinPath + @TimedMessages\timedmessages.txt
及
欢迎任何帮助 如果目录存在时不执行任何操作,则可以缩短代码。此外,不要使用所有字符串连接来拖拽代码,以创建路径和文件名。
在使用适当的方法创建文件名和路径名()输入逻辑之前,只需执行一次
如果目录存在时什么也不做,那么可以缩短代码。此外,不要使用所有字符串连接来拖拽代码,以创建路径和文件名。
在使用适当的方法创建文件名和路径名()输入逻辑之前,只需执行一次
这两种情况之间的唯一区别似乎是路径。因此,只需在if-else中获取这条路径
const string GroupsPath = @"Groups\timedmessages.txt";
const string TimedMessagesTxt = @"TimedMessages\TimedMessages.txt";
string addPath = null;
if (!Directory.Exists(MenGinPath)) {
addPath = GroupsPath;
} else if (!File.Exists(Path.Combine(MenGinPath, TimedMessagesTxt))) {
addPath = TimedMessagesTxt;
}
If (addPath != null) {
Directory.CreateDirectory(Path.Combine(MenGinPath, addPath));
File.WriteAllLines(Path.Combine(MenGinPath, TimedMessagesTxt),
new string[] { "Seperate each message with a new line" });
}
注意:使用
Path.combined
而不是字符串串联具有自动添加或删除缺失或额外\
的优点。这两种情况之间的唯一区别似乎是路径。所以只要在你的路上
const string GroupsPath = @"Groups\timedmessages.txt";
const string TimedMessagesTxt = @"TimedMessages\TimedMessages.txt";
string addPath = null;
if (!Directory.Exists(MenGinPath)) {
addPath = GroupsPath;
} else if (!File.Exists(Path.Combine(MenGinPath, TimedMessagesTxt))) {
addPath = TimedMessagesTxt;
}
If (addPath != null) {
Directory.CreateDirectory(Path.Combine(MenGinPath, addPath));
File.WriteAllLines(Path.Combine(MenGinPath, TimedMessagesTxt),
new string[] { "Seperate each message with a new line" });
}
注意:使用
Path.combined
而不是字符串串联具有自动添加或删除缺失或额外\
的优点。是否需要此工作
string strAppended = string.Empty;
if (!Directory.Exists(MenGinPath))
{
strAppended = MenGinPath + @"Groups\timedmessages.txt";
}
else if (!File.Exists(MenGinPath + @"TimedMessages\timedmessages.txt"))
{
strAppended = MenGinPath + @"TimedMessages\TimedMessages.txt";
}
else
{
return;
}
Directory.CreateDirectory(strAppended);
File.WriteAllLines(strAppended, new string[] { "Seperate each message with a new line" });
我发现,重用这样的代码块而不是将它们隐藏在if语句中是一个好主意,因为这样可以使代码维护和调试更容易,更不容易出现遗漏的bug。是否需要这样的工作
string strAppended = string.Empty;
if (!Directory.Exists(MenGinPath))
{
strAppended = MenGinPath + @"Groups\timedmessages.txt";
}
else if (!File.Exists(MenGinPath + @"TimedMessages\timedmessages.txt"))
{
strAppended = MenGinPath + @"TimedMessages\TimedMessages.txt";
}
else
{
return;
}
Directory.CreateDirectory(strAppended);
File.WriteAllLines(strAppended, new string[] { "Seperate each message with a new line" });
我发现,重用这样的代码块而不是将它们隐藏在if语句中是一个好主意,因为这样可以使代码维护和调试更容易,并且不容易出现错误。您不需要检查目录是否存在,因为如果不存在,则自动创建目录,如果目录已存在 另外,创建目录时不要包含文件名。是的,它不会出错,只是为了清楚起见 另一种方法是使用而不是硬编码路径。这将提高代码的可读性 因此,以下是我能想到的:
string dir = Path.Combine(MenGinPath, @"Groups\TimesMessages");
string file = Path.Combine(dir, "timedmessages.txt");
// this automatically creates all directories in specified path
// unless it already exists
Directory.CreateDirectory(dir);
//of course, you still need to check if the file exists
if (!File.Exists(file) {
File.WriteAllLines(filePath, new string[] { "Seperate each message with a new line" });
}
/* or if file exists, do your stuff (optional)
* else {
* //do something else? maybe edit the file?
* }
*/
您不需要检查目录是否存在,因为如果目录不存在,将自动创建目录,如果目录已经存在,则不执行任何操作 另外,创建目录时不要包含文件名。是的,它不会出错,只是为了清楚起见 另一种方法是使用而不是硬编码路径。这将提高代码的可读性 因此,以下是我能想到的:
string dir = Path.Combine(MenGinPath, @"Groups\TimesMessages");
string file = Path.Combine(dir, "timedmessages.txt");
// this automatically creates all directories in specified path
// unless it already exists
Directory.CreateDirectory(dir);
//of course, you still need to check if the file exists
if (!File.Exists(file) {
File.WriteAllLines(filePath, new string[] { "Seperate each message with a new line" });
}
/* or if file exists, do your stuff (optional)
* else {
* //do something else? maybe edit the file?
* }
*/
这段代码无法编译。请发一个@Steve,怎么样?我编译它没有问题。@Steve顺便说一句,WriteLog是我自己制作的一个方法,如果你的tryna不可能做到这一点的话。该代码包含一个else if,但前面没有正确的if。编译器在第一次运行时停止,但缺少一个}WriteAllLines@Selrygoxeser您无法让编译器接受未附加到
if
的else
。此代码无法编译。请发一个@Steve,怎么样?我编译它没有问题。@Steve顺便说一句,WriteLog是我自己制作的一个方法,如果你的tryna不可能做到这一点的话。该代码包含一个else if,但前面没有正确的if。编译器在第一次运行时停止,但缺少一个}WriteAllLines@Selrygoxeser您无法让编译器接受未附加到if
的else
。是否应添加该目录。如果需要,CreateDirectory
将创建整个目录结构?@camiloterevento更正,老实说,我还没想过。可能是因为在这个问题的背景下,它似乎无关。无论如何,提供的链接也解释了这一点。如果需要,您是否应该添加Directory.CreateDirectory
将创建整个目录结构?@camiloterEvent要更正,老实说,我没有考虑过它。可能是因为在这个问题的背景下,它似乎无关。无论如何,提供的链接也解释了这一点。谢谢你的帮助!!谢谢你的帮助!!