"지정된 경로의 형식은 지원되지 않습니다."
내 웹 서비스에 다음 코드가 있습니다.
string str_uploadpath = Server.MapPath("/UploadBucket/Raw/");
FileStream objfilestream = new FileStream(str_uploadpath +
fileName, FileMode.Create, FileAccess.ReadWrite);
누군가 코드의 2 행에서이 오류 메시지로 문제를 해결하도록 도와 줄 수 있습니까?
지정된 경로의 형식이 지원되지 않습니다.
폴더에 대한 권한은 모든 사용자에게 전체 액세스로 설정되며 폴더의 실제 경로입니다.
중단 점은 로의 값을 제공 str_uploadpath
했습니다 C:\\webprojects\\webservices\\UploadBucket\\Raw\\
.
이 문자열에 어떤 문제가 있습니까?
을 str_uploadpath + fileName
사용하는 System.IO.Path.Combine
대신 다음을 사용해보십시오 .
Path.Combine(str_uploadpath, fileName);
문자열을 반환합니다.
작성자가 전체 경로로 파일 이름을 저장하려고 할 때 오류가 발생했음을 알았습니다. 실제로이 ":"
오류를 얻으려면 파일 이름에를 포함 하는 것으로 충분합니다 . ":"
파일 이름에 있을 수있는 경우 (예 : 파일 이름에 날짜 스탬프가있는 경우) 다른 것으로 바꾸십시오. 즉 :
string fullFileName = fileName.Split('.')[0] + "(" + DateTime.Now.ToString().Replace(':', '-') + ")." + fileName.Split('.')[1];
파일 시스템에 파일을 저장하려는 경우. Path.Combine은 파일 이름에 잘못된 문자가 포함 된 경우 도움이되지 않으므로 방탄이 아닙니다. 다음은 파일 이름에서 유효하지 않은 문자를 제거하는 확장 방법입니다.
public static string ToSafeFileName(this string s)
{
return s
.Replace("\\", "")
.Replace("/", "")
.Replace("\"", "")
.Replace("*", "")
.Replace(":", "")
.Replace("?", "")
.Replace("<", "")
.Replace(">", "")
.Replace("|", "");
}
그리고 사용법은 다음과 같습니다.
Path.Combine(str_uploadpath, fileName.ToSafeFileName());
나에게 문제는 인간의 눈에 보이지 않는 ""
Left-To-Right Embedding 캐릭터였습니다.
Windows 파일 속성 보안 탭에서 경로를 복사하여 붙여 넣은 후 문자열 시작 부분 ( 'D'바로 앞)에 붙어 있습니다.
var yourJson = System.IO.File.ReadAllText(@"D:\test\json.txt"); // Works
var yourJson = System.IO.File.ReadAllText(@"D:\test\json.txt"); // Error
따라서 언뜻보기에 동일한 두 줄은 실제로 다릅니다.
이 오류를 유발할 수있는 다른 요인들
전체 PathFile 문자열에 특정 문자를 포함 할 수 없습니다.
예를 들어 다음 문자는 StreamWriter 함수를 중단시킵니다.
"/"
":"
충돌하는 다른 특수 문자도있을 수 있습니다. 예를 들어 DateTime 스탬프를 파일 이름에 넣으려고 할 때 이런 일이 발생합니다.
AppPath = Path.GetDirectoryName(giFileNames(0))
' AppPath is a valid path from system. (This was easy in VB6, just AppPath = App.Path & "\")
' AppPath must have "\" char at the end...
DateTime = DateAndTime.Now.ToString ' fails StreamWriter... has ":" characters
FileOut = "Data_Summary_" & DateTime & ".dat"
NewFileOutS = Path.Combine(AppPath, FileOut)
Using sw As StreamWriter = New StreamWriter(NewFileOutS , True) ' true to append
sw.WriteLine(NewFileOutS)
sw.Dispose()
End Using
이 문제를 방지하는 한 가지 방법은 NewFileOutS의 문제 문자를 무해한 문자로 바꾸는 것입니다.
' clean the File output file string NewFileOutS so StreamWriter will work
NewFileOutS = NewFileOutS.Replace("/","-") ' replace / with -
NewFileOutS = NewFileOutS.Replace(":","-") ' replace : with -
' after cleaning the FileNamePath string NewFileOutS, StreamWriter will not throw an (Unhandled) exception.
이것이 누군가에게 두통을 덜어주기를 바랍니다 ...!
변경 시도 :
Server.MapPath("/UploadBucket/Raw/")
에
Server.MapPath(@"\UploadBucket\Raw\")
If you get this error in PowerShell, it's most likely because you're using Resolve-Path
to resolve a remote path, e.g.
Resolve-Path \\server\share\path
In this case, Resolve-Path
returns an object that, when converted to a string, doesn't return a valid path. It returns PowerShell's internal path:
> [string](Resolve-Path \\server\share\path)
Microsoft.PowerShell.Core\FileSystem::\\server\share\path
The solution is to use the ProviderPath
property on the object returned by Resolve-Path
:
> Resolve-Path \\server\share\path | Select-Object -ExpandProperty PRoviderPath
\\server\share\path
> (Resolve-Path \\server\share\path).ProviderPath
\\server\share\path
This was my problem, which may help someone else -- although it wasn't the OP's issue:
DirectoryInfo diTemp = new DirectoryInfo(strSomePath);
FileStream fsTemp = new FileStream(diTemp.ToString());
I determined the problem by outputting my path to a log file, and finding it not formatting correctly. Correct for me was quite simply:
DirectoryInfo diTemp = new DirectoryInfo(strSomePath);
FileStream fsTemp = new FileStream(diTemp.FullName.ToString());
Does using the Path.Combine method help? It's a safer way for joining file paths together. It could be that it's having problems joining the paths together
I am using the (limited) Expression builder for a Variable for use in a simple File System Task to make an archive of a file in SSIS.
This is my quick and dirty hack to remove the colons to stop the error: @[User::LocalFile] + "-" + REPLACE((DT_STR, 30, 1252) GETDATE(), ":", "-") + ".xml"
If the value is a file url like file://C:/whatever, use the Uri class to translate to a regular filename:
var localPath = (new Uri(urlStylePath)).AbsolutePath
In general, using the provided API is best practice.
참고URL : https://stackoverflow.com/questions/7348768/the-given-paths-format-is-not-supported
'program story' 카테고리의 다른 글
파일에 새 줄을 추가 하시겠습니까? (0) | 2020.08.28 |
---|---|
안드로이드 라디오 버튼 그룹에서 라디오 버튼 아이콘을 변경할 수 있습니까? (0) | 2020.08.28 |
NSAutoreleasePool 자동 릴리스 풀은 어떻게 작동합니까? (0) | 2020.08.27 |
자바 캐스팅으로 오버 헤드가 발생합니까? (0) | 2020.08.27 |
대부분의 프린터에서 처리 할 수있는 최소 여백은 얼마입니까? (0) | 2020.08.27 |