如果我有两个DirectoryInfo对象,我如何比较它们的语义相等?例如,以下路径应全部视为等于C:\temp:
C:\tempC:\temp\C:\temp\.C:\temp\x\..\..\temp\.以下可能等于或不等于C:\temp:
\temp 如果当前工作目录在驱动器上 C:\temp 如果当前工作目录是 C:\C:\temp.C:\temp...\如果考虑当前的工作目录很重要,我可以自己解决这个问题,所以这并不重要.尾随点在窗口中被剥离,因此这些路径确实应该相等 - 但它们不会在unix中被剥离,所以在mono下我会期望其他结果.
区分大小写是可选的.路径可能存在也可能不存在,用户可能拥有或可能没有路径权限 - 我更喜欢快速健壮的方法,不需要任何I/O(所以没有权限检查),但是如果有什么内置的话 - 我对任何"足够好"的东西都很满意......
For a long path aware process on Windows 10, I'm trying to understand what the argument restrictions are when using the windows shell method PathRelativePathTo.
In my example below, I'm using C# via pinvoke to call the method.
I've given multiple examples below and their output. Note:
如何通过非规范的方式获取规范文件名.
例如,我想调用转换"C:\Program files\..\Windows\aaa.txt"为的函数"C:\Windows\aaa.txt"
我正在寻找类似Java File.getCanonicalPath()的东西
我试图以编程方式创建目录的快捷方式.我找到了很多例子,但似乎没有一个例子可行.
我在生成的快捷方式属性中观察到三个不同的结果:
文件的快捷方式类型被指定为"快捷方式(.lnk)",这会导致弹出打开对话框,要求我附加一个扩展名.
"文件"的"快捷方式"属性被指定为"文件",双击时绝对没有任何内容.
或者最后,这当然是我最喜欢的......文件的快捷方式属性被指定为:"文件夹",它的工作方式应该如此.
这是我目前正在使用的代码......我尝试了一些这方面的变化.
bool IsExists = false;
string icon = appPath + "Welcome.ico";
// Their is a difference to local and ClickOnce App locations... this compensates for it
IsExists = System.IO.File.Exists(icon);
if (!IsExists)
{
icon = appPath + @"bin\Debug\Welcome.ico";
}
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var target = (Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Artronix\Welcome To FreightWare Online\").Replace(@"\","/");
IWshShortcut shortcut;
var wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(Path.Combine(desktop, @"FreightWare Resources.lnk"));
shortcut.IconLocation = icon;
shortcut.TargetPath = Path.GetFullPath(target);
shortcut.Save();
Run Code Online (Sandbox Code Playgroud) 今天早些时候我正在调试有点像这样的东西:
class Foo {
void AccessCriticalSection() {
try {
if (IO.File.Exists("\\path\to\lock.txt")
throw new Exception();
System.IO.File.Create("\\path\to\lock.txt");
criticalSection();
} catch (Exception ex) {
// ignored
}
}
void CriticalSection() {
// banana banana banana
System.IO.File.Delete("\\path\to\lock.txt");
}
}
Run Code Online (Sandbox Code Playgroud)
让我们甚至不知道这是多么可怕......但它本质上是试图使用一个名为lock.txtmutex 的文件.该操作不是原子操作,如果另一个进程正在使用它,其他进程只是没有通过关键部分(如果您可以相信它们,它们的目的是能够在锁定释放后继续)等等.显然它需要修复.
如何正确获取锁以跨多个进程同步对文件系统的访问?这些进程是同一进程的多个实例,因此它们可以共享一些协议而不是专门锁定目录(即,它们可以轻松地使用等同于某些类锁定的所有实例的东西,例如private final static Object lock = new Object();同步访问静态方法)