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);
Related
string ext = ".ext";
RegistryKey key = Registry.ClassesRoot.CreateSubKey(ext);
MessageBox.Show(exePath);
key.SetValue("", "My Project");
key.Close();
key = Registry.ClassesRoot.CreateSubKey(ext + "\\Shell\\Open\\command");
//key = key.CreateSubKey("command");
key.SetValue("", "\"" + Application.ExecutablePath + "\" \"%L\"");
key.Close();
key = Registry.ClassesRoot.CreateSubKey(ext + "\\DefaultIcon");
key.SetValue("", Application.StartupPath + "\\icon.ico");
key.Close();
i wanna create a new file extension like "prproj" primiere pro in c#,for example you save that file and you wanna open it again and when you clicked at file, it will be opened in your application and also, i want to know how to give a icon file to that file?
please help!
You can use Setup Wizard, in it you can change the parameter that is responsible for the file extension belonging to your program.
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
Actually I'm trying to create zip file of a directory and but the ZipFile.CreateFromDirectory() giving below Exception.
System.IO.IOException : The process cannot access the file
PATH_TO_CREATE_ZIP/file.zip' because it is being used by another
process.
Following is the Code Snippet for it. :
public void createZipFile(string zipPath, string archiveFileName)
{
string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;
if (Directory.Exists(DirectoryToBeArchive + ".zip"))
{
File.Delete(DirectoryToBeArchive);
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
}
else
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
Directory.Delete(DirectoryToBeArchive);
}
Help Would be Much Appreciated. Thanks in Advance. :)
It only makes sense you get this exception. Let's investigate your code step by step:
createZipFile("C:\\Temp", "myZipFile");
public void createZipFile(string zipPath, string archiveFileName)
{
//DirectoryToBeArchive = "C:\\Temp\\myZipFile"
string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;
//Some logical error here, you probably meant to use File.Exists()
//Basically, as you can't find a directory with name C:\\Temp\\myZipFile.zip, you always jump into else
if (Directory.Exists(DirectoryToBeArchive + ".zip"))
{
File.Delete(DirectoryToBeArchive);
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
}
else
//It will try to overwrite your existing "DirectoryToBeArchive".zip file
ZipFile.CreateFromDirectory(zipPath, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
//This won't work as well btw, as there probably is no directory
//with name C:\\Temp\\myZipFile
Directory.Delete(DirectoryToBeArchive);
}
Though, even if you delete the file, you will probably hit same error.
The thing is when you try zipping the folder C:\\Temp into the file C:\\Temp\\myZipFile.zip you will also try zipping the file itself. That's actually where you get the file is being used error.
So,
Replace Directory.Exists() with File.Exists()
Zip in another folder
Just a friendly warning, I'd be cautious with Directory.Delete() if I were you :)
My issue was, the output folder and zipping folder was the same.
Moved to separate folders, now working fine.
Correct Code :
this piece of code after little correction worked for me..
string DirectoryToBeArchive = zipPath + "\\" + archiveFileName;
if (File.Exists(DirectoryToBeArchive + ".zip"))
{
File.Delete(DirectoryToBeArchive + ".zip");
ZipFile.CreateFromDirectory(DirectoryToBeArchive, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
}
else
ZipFile.CreateFromDirectory(DirectoryToBeArchive, DirectoryToBeArchive + ".zip", CompressionLevel.Fastest, false);
Directory.Delete(DirectoryToBeArchive , true);
string UserFolder = Session["Username"].ToString();
if (!Directory.Exists("~/MisReports/EmailAttachment/"+UserFolder))
{
Directory.CreateDirectory("~/MisReports/EmailAttachment/"+UserFolder);
}
filePathE = Server.MapPath("~/MisReports/EmailAttachment/" + UserFolder + "/");
filePathE = filePathE + a + ".pdf";
bool isExist = File.Exists(filePathE);
if (isExist)
{
File.Delete(filePathE);
}
report.ExportToDisk(ExportFormatType.PortableDocFormat, filePathE);
I get the error
Could not find a part of the path
if (!Directory.Exists("~/MisReports/EmailAttachment/"+UserFolder))
{
Directory.CreateDirectory("~/MisReports/EmailAttachment/"+UserFolder);
}
in this area code does not enter if check although that the folder has not been created
The ~ is probably not evaluated if you are running on Windows. Windows is not Unix. Read up on Path.Combine, Environment.GetFolderPath and Environment.SpecialFolder on MSDN. You should build a path with code like
string directoryName = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "MisReports/EmailAttachment", UserFolder);
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.