我需要使用C#創建帶尾點(。)的文件夾。這是我的代碼:如何在.NET/C#中創建帶尾點(。)的文件夾?
System.IO.Directory.CreateDirectory("d:\\MyCorp Inc.");
但是該文件夾創建時沒有最後一個點。有解決方案嗎?
我正在使用.NET Framework 4.5/C#。我的文件系統是NTFS,在Windows上 8.
我需要使用C#創建帶尾點(。)的文件夾。這是我的代碼:如何在.NET/C#中創建帶尾點(。)的文件夾?
System.IO.Directory.CreateDirectory("d:\\MyCorp Inc.");
但是該文件夾創建時沒有最後一個點。有解決方案嗎?
我正在使用.NET Framework 4.5/C#。我的文件系統是NTFS,在Windows上 8.
嘗試:
using System.Runtime.InteropServices;
class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CreateDirectory(string lpPathName, IntPtr lpSecurityAttributes);
private static void Main(string[] args)
{
CreateDirectory(@"\\?\c:\temp\MyCorp Inc.", IntPtr.Zero);
}
,並參考You cannot delete a file or a folder on an NTFS file system volume。
對不起!投擲:「路徑中的非法字符。」 win7,4.0 –
對不起!那是我試過的那個! –
不得不繞過.NET並直接進入Windows API,因爲API正在進行額外驗證。測試了我的機器上的代碼! :) –
這不是對.NET的限制,而是對Win32 API的限制。嘗試在命令外殼中創建一個:
c:\drop>mkdir test.
c:\drop>dir
Volume in drive C has no label.
Volume Serial Number is C0F3-338B
Directory of c:\drop
2013-04-02 17:54 <DIR> .
2013-04-02 17:54 <DIR> ..
2013-04-02 17:54 <DIR> test
有關命名建議的信息,請參見MSDN。引用:
•不要用空格或句點結尾文件或目錄名稱。 儘管底層文件系統可能支持這些名稱,但Windows外殼和用戶界面不支持。但是,可以接受 指定句點作爲名稱的第一個字符。例如, 「.temp」。
如果你確實堅持使用尾點,使用\\?\C:\path.
。
c:\drop>mkdir \\?\c:\drop\test2.
c:\drop>dir
Volume in drive C has no label.
Volume Serial Number is C0F3-338B
Directory of c:\drop
2013-04-02 17:58 <DIR> .
2013-04-02 17:58 <DIR> ..
2013-04-02 17:54 <DIR> test
2013-04-02 17:58 <DIR> test2.
編輯:在內容中列出的要求是沒有額外的文件被創建,所以不要使用一個文件。您可以在目錄上創建一個「名稱」流並在其中存儲數據。例子請參閱以下內容:
c:\drop>mkdir "Example Company Name"
c:\drop>notepad "Example Company Name:name.txt"
c:\drop>dir
Volume in drive C has no label.
Volume Serial Number is C0F3-338B
Directory of c:\drop
2013-04-02 18:25 <DIR> .
2013-04-02 18:25 <DIR> ..
2013-04-02 18:25 <DIR> Example Company Name
0 File(s) 0 bytes
3 Dir(s) 77,336,379,392 bytes free
c:\drop>dir "Example Company Name"
Volume in drive C has no label.
Volume Serial Number is C0F3-338B
Directory of c:\drop\Example Company Name
2013-04-02 18:25 <DIR> .
2013-04-02 18:25 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 77,336,313,856 bytes free
或者在.NET:
class Program
{
static void Main(string[] args)
{
string companyName = "Example Company?*, Inc.";
//Example Company Inc
var sanitizedName = sanitize(companyName);
//Create the directory
Directory.CreateDirectory(sanitizedName);
//Create the name store
var nameStreamPath = sanitizedName + ":Name";
writeFileContent(companyName, nameStreamPath);
//Try to return the name
Console.WriteLine(getFileContent(nameStreamPath));
Console.ReadLine();
}
private static string getFileContent(string path)
{
using (var sr = new StreamReader(new FileStream(
// we have to call CreateFile directly to avoid overzealous path validation
NativeMethods.CreateFileOrFail(path, false), FileAccess.Read)))
{
return sr.ReadToEnd();
}
}
private static void writeFileContent(string companyName, string path)
{
using (var sw = new StreamWriter(new FileStream(
// We have to call CreateFile directly to avoid overzealous path validation
NativeMethods.CreateFileOrFail(path, true), FileAccess.Write)))
{
sw.Write(companyName);
}
}
private static string sanitize(string path)
{
char[] newPath = new char[path.Length];
int newPathLoc = 0;
for (int i = 0; i < path.Length; i++)
{
if (char.IsLetter(path[i]) || char.IsDigit(path[i]))
{
newPath[newPathLoc] = path[i];
newPathLoc++;
}
}
return new string(newPath, 0, newPathLoc);
}
}
class NativeMethods
{
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern SafeFileHandle CreateFile(
string fileName,
[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flags,
IntPtr template);
public static SafeFileHandle CreateFileOrFail(string path, bool write)
{
var handle = CreateFile(path,
write ? FileAccess.Write : FileAccess.Read,
write ? FileShare.None : FileShare.Read,
IntPtr.Zero,
write ? FileMode.Create : FileMode.Open,
FileAttributes.Normal,
IntPtr.Zero);
if (handle.IsInvalid)
throw new System.ComponentModel.Win32Exception();
return handle;
}
}
Downvoter,關心評論? – Mitch
如果試圖在Windows資源管理器手動做到這一點甚至Explorer不允許尾隨點,並刪除它。
我想這是Windows本身的侷限性,因爲最後一段時間通常表示文件擴展名。
在這個時候我會說這是不可能的。
微軟明確指出,這是一件不應該做的:http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
不要結束一個文件或目錄名稱用空格或句點。儘管底層文件系統可能支持這種名稱,但Windows外殼程序和用戶界面不支持。但是,可以將句點指定爲名稱的第一個字符。例如,「.temp」。
更新時間:
我注意到兩件事情!
1)您的需求:
我有根據公司 的名字命名他們的文件夾,例如「有些公司有限公司」很多客戶,這些名字往往包含尾隨 點。我不能在別處存儲名稱,我沒有數據庫,並且我不能創建額外的文件,這是要求。
2)你還沒有得到一個答案
我的建議是在目錄名的結束,這將被隱藏到普通視圖中添加額外的性格,但仍然存在!這讓我們開心!
試試這個
你必須在目錄名
System.IO.Directory.CreateDirectory
(@"D:\Sync.inc." + (char)(128));
末注意追加一個特殊字符(字符)( 128)到(char)(158)可能有助於
試試吧!默認情況下
如果點位於該名稱的末尾,Windows會忽略點(S) :
for (int i = 128; i <= 158; i++) { System.IO.Directory.CreateDirectory (@"D:\Sync.inc." + (char)(i)); }
反正從MS。
你爲什麼要這麼做?這不是C#和.NET的限制,而是您正在使用的操作系統。在探險家中嘗試一下。 – walther
VIsual studio引發異常,你不應該這樣做,但你可以使用cmd: mkdir \\?\ d:\ MyCorp Inc. 你會注意到你不能刪除它: rmdir \\? \ d:\ MyCorp Inc. –
「你爲什麼要這麼做?」我有很多客戶根據公司名稱命名文件夾,例如「Some Corp Ltd.」,這些名稱通常包含尾隨點。我無法在其他地方存儲名稱,我沒有數據庫,也無法創建額外的文件,這是要求。 –