When you need to upload a file using IE, a file browsing dialog will open and let you choose a file.
In future whenever you try to upload a file, previously selected file's location will be displayed as first folder in file browsing dialog.
After some researches I find out opened file's information will save in this location of registry:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU
I need to Change IE default File Browsing Folder from code.
Related
I can't delete a pdf file from a shared drive (Windows mapped drive) from my c# Winform desktop program which runs on Windows 10 OS. The pdf file was created by Crystal Report in my c# program and when there is a revision, I need to delete the old pdf file and write the new one. I will get access denied error (HResult=0x80070005) when the program tries to delete the file. This only happens when the I try to delete it from a shared drive. If I generate the pdf file in the local drive (c:), and delete it from the local drive, then everything is find. I can manually delete the pdf file from File Explorer, but not from my c# program.
In addition, if the file is not a pdf file, then there is no problem to delete it from the shared drive.
It is weird that this only happens to pdf file or a file with .pdf extension. If I create a simple text file (test.txt) with C++ and save it in the shared drive, I can delete it without any problem. But when I create the same text file and name it as test.pdf, then I can't delete it.
HResult=0x80070005 seems only links to permission issue, but I strongly believe my case is NOT a permission issue because if the file is *.txt, then I can always delete it without any issues.
To rule out the possibility of the pdf file that may be still linked with the Crystal Report or opened by some programs, I copied the pdf file from the shared drive and save it in my local drive (c:) and wrote a simple c++ program to test if I can delete it, and it failed. Can't delete it. I can only delete it if the pdf file was created and saved in the local drive originally. And if the pdf file is copied from the shared drive to the local drive, then I can't delete it.
I would appreciate it if someone can shed some light on it.
I think (you/your app) don't Have Permission To Delete a File
Go to the folder that contains the PDF File.
Right-click on that folder and choose Properties.
Navigate to the Security tab.
Click on Edit, then click Add.
Under the Enter the object names to select box. Type
"Everyone"(without quotes) and click on Check names. Click OK.
A new window will open up with the Security tab. Under Permissions for
Everyone, check the Full control option. Select Apply and OK.
Ownership is already allowed for the folder.
Now you Can delete PDF files programmatically
Don't Have Permission To Delete a Folder
Thanks for trying to provide solutions for this problem. With a help from a friend, I finally found the root cause of the problem. It is the Norton antivirus that prevents me from deleting a certain types of files. .pdf is one of them. After I unchecked the .pdf from Norton, it works like a magic.
It is the antivirus program that prevents me from deleting .pdf file programmatically.
I have a problem with Directory.GetFiles(). I get all file with it, but I have a file doesn't not exist in my folder (file like ~$temp.docx), so what's that problem ?
This is my code. Thanks
string[] files = Directory.GetFiles(Server.MapPath(path), "*.*", SearchOption.AllDirectories);
~$temp.docx is a hidden file. If you dont need any of the hidden files in a folder you could just exclude them. Which is discussed in this thread: C# - Get a list of files excluding those that are hidden
I'd wager that there IS a file like that in your directory, but you can's see it with File Explorer. That kind of file is a hidden file that is created when you have a word document open in Microsoft Word. Windows File Explorer doesn't show those files by default when you open up a folder and look at the files. You need to go to View -> Options -> Advanced Settings -> Show Hidden Files and set it to show hidden files. Then you'll see that file with File Explorer as well as your C# code.
I want to show a local html file which exists in my project:
The calling file is the HelpFile.cs and in that the form contains the WebBrowser control.
The address I'm trying to reach is:
C:\Users\Keith\Desktop\Lingerie\Corset\Corset\Bra.html
The file is being called from:
The result is the dreaded page can't be displayed.
What I would like to do is be able to call the file as a relative html page. At a later stage, I would like to be able to call different help files.
Is this the best way to proceed or have I made a fundamental error and gone down the wrong path?
Any constructive help would be appreciated.
A file which exists in your project, lives in a specific location in your machine. But after you distribute the program and run it on user's machine, the file will not exists in the target machine.
You may want to distribute the file or add it as resource. To solve the problem you can use either of the following solutions:
You can copy the file to output directory at build time
You can add the file to a resource file like Resources.resx
You can make the file as an embedded resource
Then to show the file, you can use the following methods:
Get the file path and call the Navigate method or assign it to Url property
Get the resource content and assign it to DocumentText property
Get the resource stream and assign it to DocumentStream property
Copy the file to Output Directory
To copy the file to output directory at build time:
Solution explorer → See properties of your file
Set Build Action to Content.
Set Copy to Output Directory to Copy always.
Then the file will be copied to your output directory and you can use it this way:
var path = System.IO.Path.Combine(Application.StartupPath, "test.html");
this.webBrowser1.Navigate(path);
Please note, if the file is located under a folder in your project, for example under MyFolder, then it will be copied into a folder with the same name in the output directory of the application:
var path = System.IO.Path.Combine(Application.StartupPath, "MyFolder", "test.html");
this.webBrowser1.Navigate(path);
Add the file to a resx resource file like Resources.Resx
You can add the file to resource file of the project. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Solution explorer → Your project → Properties folder → open Resources.Resx file
From toolbar of the designer → Add existing file → Add the html file.
Then the content of the file will be available through a string property of the Resources. The property name will be same as the file name, for example if the file name is test.html, the property name will be test and You can use it this way:
this.webBrowser1.DocumentText = Properties.Resources.test;
Please note, for this solution the file doesn't need to be distributed by your project and it will be part of the resource file. However it will be part of your project file.
Make the file as an embedded resource
You can make the file as an embedded resource. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Solution explorer → See properties of your file
Set Build Action to Embedded Resource.
Set Copy to Output Directory to Do not copy.
Then to use, you need to get the file content from embedded resources. Assuming the file name is "test.html":
var fileName = "test.html";
var name = Assembly.GetExecutingAssembly().GetManifestResourceNames()
.Where(x => x.EndsWith(fileName)).First();
webBrowser1.DocumentStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
Please note, if you have the file inside a folder like MyFolder in the project, then the filename in above example will be "MyFolder.test.html".
I have a directory containing files (images , pdf ..etc).. then I've got all the directory files with c# code and viewed them as a hyperlink for each file to be able to view each file in the browser.
when I click on the pdf file link for example , it doesn't view the file .. but when I copy the link address of the pdf file , then paste and view it in a separated window . the pdf file open normally.
when I view the source code of the page I find that the link for the pdf file as it is in my local machine directory not the server one.
Please help me with that..
Thanks.
Its a security issue. In console, you must be seeing the error "Not allowed to load local resource". On clicking the anchor tag, which contains the absolute path to local file, the file has to be served by the IIS server(as the page is web page, which is loaded from asp.net application), which is not allowed.
In a web application, if you provide local file path to a file in your webpages, you are making the assumption that the file exists on the clients computer or device and not on your server.
So, create a folder inside your applications root folder and just give relative path to those files in anchor tag that includes hostname like
http://myserver.com/aspnet.pdf
The default downloads folder in Windows 7 is c:\users\username\downloads for Chrome. But default downloads folder can be set to another folder through settings. I need to find out which folder is the downloads folder. So if I set the downloads folder to c:\dd, then I need to find out the specific folder.
Is there a way of finding this out using C#? I mean I don't want to visit the chrome settings and fetch the folder path from there.
The default locations for Chrome settings are here. I actually found mine to be under 'C:\Users[UserName]\AppData\Local\Google\Chrome\User Data\Default' for Chrome 20.0.1132.47.
If you open the Preferences file, you can parse the JSON to get the download folder:
"download": {
"default_directory": "C:\\Users\\[UserName]\\Downloads",
"directory_upgrade": true,
"extensions_to_open": ""
}
If there is no default_directory value, then the directory C:\Users\[UserName]\Downloads is being used.