I have some json files that I need to read from my asp.net core app. They are under a folder called data
--MyProject
---Startup.cs
---Data
------dataset1.json
------dataset2.json
I am using IHostingEnvironment ContentRootPath to read the files:
string pathToFile = hostingEnvironment.ContentRootPath
+ Path.DirectorySeparatorChar
+ "Data"
+ Path.DirectorySeparatorChar
+ "dataset1.json"
which returns C:\SourceControl\Test.Backend\src\Test.Web\Data\dataset1.json
This works fine when I publish my code in IIS. However when I am debugging the files are copied into bin folder and the above code does not work.
How can I read the files while debugging?
#if DEBUG
string pathToFile = hostingEnvironment.ContentRootPath
+ Path.DirectorySeparatorChar
+ "bin"
+ Path.DirectorySeparatorChar
+ "Data"
+ Path.DirectorySeparatorChar
+ "dataset1.json"
#else
string pathToFile = hostingEnvironment.ContentRootPath
+ Path.DirectorySeparatorChar
+ "Data"
+ Path.DirectorySeparatorChar
+ "dataset1.json"
#endif
you can have two different paths when you are in debug mode and in production with this approach. Just change the first path to your needs
Related
I try to display RTSP stream using Gstreamer in my WPF application.
So I did so far:
installed GStreamer into loal folder F:/gstreamer
Created new WPF application
Added glib-sharp and gstreamer-sharp as dependencies.
The code below I use to init the library:
Gst.Application.Init(); // (1)
mainLoop = new GLib.MainLoop();
mainGLibThread = new System.Threading.Thread(mainLoop.Run);
mainGLibThread.Start();
Element uriDecodeBin = ElementFactory.Make("playbin", "uriDecodeBin0"); // (2)
Unable to load DLL 'libgstreamer-1.0-0.dll': The specified module could not be found.
on line (1). If I copy all the gstreamer dlls into bin/Debug folder the exception gone but ElementFactory.Make in line (2) always returns null without any exception. If I try to do something like
Parse.Launch(#"videotestsrc ! videoconvert ! autovideosink")
to test the library functionality I get error:
no element "videotestsrc"
but if I run it from command line:
gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink
that works as expected.
So my question - how to get GStreamer-sharp work?
Change path to GStream and add this code before start
string path = #"G:\gstreamer\1.0\x86\"; // path to your gstream
string pluginpath = #"G:\gstreamer\1.0\x86\lib\gstreamer-1.0\"; // path to your gstream
string registry = System.IO.Path.Combine(path, "registry.bin");
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + path);
Environment.SetEnvironmentVariable("GST_PLUGIN_PATH", pluginpath + ";" + Environment.GetEnvironmentVariable("GST_PLUGIN_PATH"));
Environment.SetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH_1_0", pluginpath + ";" + Environment.GetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH_1_0"));
Environment.SetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH", pluginpath + ";" + Environment.GetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH"));
Environment.SetEnvironmentVariable("GST_DEBUG", "*:4");
Environment.SetEnvironmentVariable("GST_DEBUG_FILE", System.IO.Path.Combine(path, "gstreamer.log"));
Environment.SetEnvironmentVariable("GST_REGISTRY", registry);
I am trying to create folders in nested manner.
if (file.ContentLength > 0 && file != null)
{
string path = "~/Videos/" + Session["username"] + "_" + Session["userid"];
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filename = path + file.FileName;
filepath = "/Videos/" + Session["username"] + "_" + Session["userid"];
file.SaveAs(filename);
If you see here- /Videos/ folder is what I have currently on disk. Where another folder with user's name and id is what I want to create inside this Videos folder. How Would I be creating this folder inside this folder?
Because currently it is showing me this error -
Access to the path '~/Videos/shaun_2' is denied.
I tried restarting visual studio with administrator's credentials. But it still remains here.
I'm assuming that you are using ASP.NET: try to use Server.MapPath("~/...") to get the physical path
See MSDN
I'm creating an application where I'm copying the file to the user's appdata and then adding a registry key so that the file runs at startup.
I get a URI exception while running it.
Here's the fragment of the code that's giving me trouble..
RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\My_Application");
if (rk != null)
{
//Do nothing as the program is already added to startup
}
else
{
string newpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\My_Application\\" + "My_Application.exe";
File.Copy(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString(), Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\My_Application\\" + "My_Application.exe");
RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
startup.SetValue("My_Application", "\"" + newpath);
}
The problem is in your startup.SetValue(). You are escaping a " character and I think you want to escape a \:
startup.SetValue("My_Application", "\\" + newpath);
If you actually mean to escape a " then you probably need one on both sides:
startup.Setvalue("My_Application", "\"" + newpath + "\"");
Or Typically this should work (I'm not super familiar with this API)
startup.SetValue("My_Application", newpath);
Use System.IO.Path.Combine for path concatenation. Also, make sure that the destination directory exists before copying.
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string newpath = System.IO.Path.Combine(appData, "My_Application", "My_Application.exe");
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(newpath));
File.Copy(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString(), newpath);
RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
startup.SetValue("My_Application", newpath);
I am trying to download a file stored in a folder.
string path = "D:\\app_data\\Clients\\Client " + jobdescription.ClientID + "\\Job " + jobdescription.JobDescriptionID + "\\";
string file = Path.Combine(path, jobdescription.JobTitle + ".docx");
return File(path, "application/docx", jobdescription.JobTitle + ".docx");
The error generated is:
Server Error in '/' Application.
Could not find a part of the path 'D:\app_data\Clients\Client 1\Job 2\'
But the file with specified filename is in the folder. What am i doing wrong ?
You use path instead of using file, in your third line.
It should read:
return File(file, "application/docx", jobdescription.JobTitle + ".docx");
The odd thing is that something like System.IO.File.Delete() works
and the file gets deleted but will give "access to path is denied error" for .Move() operation.
All files are located in the same folder, user "Network service" has all
full control rights for the folder and all subfolders in it etc.
Folders are located in the project directory and can be seen in solution explorer.
Exception Details: System.UnauthorizedAccessException: Access to the path is denied.
foreach(var info in FileActions.Where(x => x.OldSortOrder != x.SortOrder))
{
string FileToRename;
string NewName;
string OldFilePath;
string OldFileThumbPath;
FileToRename = info.ProductID + "/" + info.OldSortOrder + "-" + info.ImageID + ".jpg";
NewName = info.SortOrder + "-" + info.ImageID + ".jpg";
OldFilePath = System.Web.HttpContext.Current.Request.MapPath("~/Content/ProductImages/" + FileToRename);
OldFileThumbPath = System.Web.HttpContext.Current.Request.MapPath("~/Content/ProductImages/" + info.ProductID + "/thumbs/" + FileToRename);
System.IO.File.Move(OldFilePath, NewName);
System.IO.File.Move(OldFileThumbPath, NewName);
}
Its because you map the path for the first files but not for the NewName.
So did not have the full path to know what to rename/move the file, and needs the full path to work correctly.
With out the path this is probably try to move it on the default folder of the asp.net pool that is probably don't have this permissions.
So the code will be
NewName = System.Web.HttpContext.Current.Request.MapPath("~/Content/ProductImages/"
+ info.SortOrder + "-" + info.ImageID + ".jpg" );
and debug this lines to see if the directories and files are all correct.