Skip to content

Commit dc44115

Browse files
committed
Fix for file creation with bad filename
1 parent b1f5855 commit dc44115

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

ClipboardCanvas/Helpers/Filesystem/FilesystemOperations.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static async Task<SafeWrapper<StorageFile>> CreateFile(string path, Creat
126126
return new SafeWrapper<StorageFile>(null, parentFolder.Details);
127127
}
128128

129-
string fileName = Path.GetFileName(path);
129+
string fileName = RemoveIllegalFileNameCharacters(Path.GetFileName(path));
130130

131131
return await CreateFile(parentFolder, fileName, collision);
132132
}
@@ -138,7 +138,7 @@ public static async Task<SafeWrapper<StorageFile>> CreateFile(StorageFolder pare
138138
return new SafeWrapper<StorageFile>(null, OperationErrorCode.InvalidArgument, new ArgumentNullException(), "The provided folder is null.");
139139
}
140140

141-
return await SafeWrapperRoutines.SafeWrapAsync(() => parentFolder.CreateFileAsync(fileName, collision).AsTask());
141+
return await SafeWrapperRoutines.SafeWrapAsync(() => parentFolder.CreateFileAsync(RemoveIllegalFileNameCharacters(fileName), collision).AsTask());
142142
}
143143

144144
public static async Task<SafeWrapper<StorageFolder>> CreateFolder(string path, CreationCollisionOption collision = CreationCollisionOption.GenerateUniqueName)
@@ -165,5 +165,10 @@ public static async Task<SafeWrapper<StorageFolder>> CreateFolder(StorageFolder
165165

166166
return await SafeWrapperRoutines.SafeWrapAsync(() => parentFolder.CreateFolderAsync(name, collision).AsTask());
167167
}
168+
169+
private static string RemoveIllegalFileNameCharacters(string dangerousFileName)
170+
{
171+
return string.Join("_", dangerousFileName.Split(Path.GetInvalidFileNameChars()));
172+
}
168173
}
169174
}

0 commit comments

Comments
 (0)