I have a wpf application in which I want to save a file in the Images folder, which is on the same level with the bin folder.
How do I access that folder?
I try like this:
using (var imageFile = new FileStream((new Uri(#"pack://application:,,,/Images/Application/")).ToString(), FileMode.Create))
{
//
}
and I get the error that "The given path's format is not supported.". How can I access that folder?
Your path refers to resources within the project assembly. You should use relative or absolute file system paths:
var path = Path.Combine(
Path.DirectorySeparatorChar.ToString(),
AppDomain.CurrentDomain.BaseDirectory,
"Images"); // Result could be for example: "C:\MyWorkspace\MyProject\bin\Debug\Images"
using (var imageFile = new FileStream(path, FileMode.Create)) { }
How about using double dots(..) to get to parent directory.
using (var imageFile = new FileStream("..\..\[Path goes here]", FileMode.Create))
{
//
}
Related
I am opening a FileDialog, to let the user select an Audio file, in an MP3 file, and convert it to WAV, the error appears when I am trying to save the file in a new folder that I am creating
var dlg = new OpenFileDialog
{
DefaultExt = ".mp3",
Filter = "Audio files (.mp3)|*.mp3"
};
var res = dlg.ShowDialog();
if (res! == true)
{
var projectPath = Directory.GetParent(Directory.GetCurrentDirectory())?.Parent?.Parent?.FullName;
var FoderName = Path.Combine(projectPath!, "Audios");
Directory.CreateDirectory(FoderName);
using (var mp3 = new Mp3FileReader(dlg.FileName))
{
using (var ws = WaveFormatConversionStream.CreatePcmStream(mp3))
{
WaveFileWriter.CreateWaveFile(FoderName, was) // Error
System.UnauthorizedAccessException: 'Access to the path
'C:\XXX\XXX\XXX\XXX\XXX\XXX' is denied.'
}
}
Thanks
Inside some folders including "Program Files", an app usually has no permission to write a file. Therefore, it is recommended to use a folder dedicated for a specific purpose and made available for apps (you can get path to "Music" by Environment.GetFolderPath(Environment.SpecialFolder.MyMusic).
In general, an app must expect UnauthorizedAccessException when attempting to write a file and prepare a fallback for the case of that exception.
I need to add some ResourceDictionary to a WPF window.
If I do as follow, everything works:
var uri = $#"..\..\..\Assets\Styles";
if (Directory.Exists(uri))
{
var allFile = Directory.GetFiles(uri);
if (allFile == null) return;
foreach (var file in allFile)
{
backupManagementWindow.MergeResourceDictionary(new Uri(file, UriKind.RelativeOrAbsolute));
}
}
Since that I don't want to define the Uri based on my executable directory, I define it as follow:
var uri = AppDomain.CurrentDomain.BaseDirectory + #"Assets\Styles";
The Uri is correct (there are the files) but then the code raise an exception.
public void MergeResourceDictionary(Uri uriResource)
{
var newResource = new ResourceDictionary {Source = uriResource};
Resources.MergedDictionaries.Add(newResource);
}
Does anyone know why? Thanks!
You can use Pack Uri Scheme when working with resource dictionaries.
And your uri would look something like this:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
public void Uploader(string filename, Stream Data)
{
BinaryReader reader = new BinaryReader(Data);
string path = #"C:/Friendisc/Images";
FileStream fstream = new FileStream(path, FileMode.CreateNew);
BinaryWriter wr = new BinaryWriter(fstream);
wr.Write(reader.ReadBytes((int)Data.Length));
wr.Close();
fstream.Close();
Data.Close();
}
I am getting the error: Access to the path is denied.
What do I need to do?
Also How would I upload the image on another project within the same solution?
File path was not proper. Issue Resolved.
This will be simple for you guys:
var uri = new Uri("pack://application:,,,/LiftExperiment;component/pics/outside/elevator.jpg");
imageBitmap = new BitmapImage();
imageBitmap.BeginInit();
imageBitmap.UriSource = uri;
imageBitmap.EndInit();
image.Source = imageBitmap;
=> Works perfectly on a .jpg with
Build Action: Content
Copy to Output Directory: Copy always
MediaPlayer mp = new MediaPlayer();
var uri = new Uri("pack://application:,,,/LiftExperiment;component/sounds/DialingTone.wav");
mp.Open(uri);
mp.Play();
=> Does not work on a .wav with the same build action and copy to output. I see the file in my /debug/ folder..
MediaPlayer mp = new MediaPlayer();
var uri = new Uri(#"E:\projects\LiftExp\_solution\LiftExperiment\bin\Debug\sounds\DialingTone.wav");
mp.Open(uri);
mp.Play();
=> Works perfectly..
So, how do I get the sound to work with a relative path? Why is it not working this way?
Let me know if you want more code or screenshots.
Thanks.
The pack://application URI syntax is for "embed" files, make sure the the media file is set to that, or use the pack://siteoforigin for "loose" files (copied to bin directory).
MSDN link
So I am trying to create an image gallery. My AIR application accepts files that are dragged and dropped onto a TileList component. I am using the images as icons but the problem is that they take a long time to load so i want to compress the file data first (I have that part done) The problem is that I can't figure out how to open the file and put the data into a BitmapData object.
Any ideas?
var req:URLRequest = new URLRequest(value.file.url);
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
ldr.load(req);
.
.
.
private function completeHandler(event:Event):void {
var ldr:Loader = Loader(event.target.loader);
var b:Bitmap = Bitmap(ldr.content);
}