I was able to open a directory on my computer locally but when I deployed my website to GoDaddy, I can't get it open. I am assuming it is some sort of security control. Here are the following actions I made:
set trust level to full in the web.config
set security to full control in my C:\ directory
Here is the following code to open a folder created in that directory:
protected void btnOpenFolder_Click(object sender, EventArgs e)
{
//Path to the customer's folder
string FolderPath = #"C:\" + customerId.ToString().PadLeft(4, '0') + " - " + lblName.Text;
if (System.IO.Directory.Exists(FolderPath))
{
//Opens the customer's folder
System.Diagnostics.Process.Start(FolderPath);
}
else
{
Response.Write("<script>alert('Must have the Dropbox directory in order to open the customer folder.');</script>");
}
}
Any suggestions would be great. Thanks in advance!
Related
I have a problem in uploading a file in ASP.NET web application. I am not able to get the correct physical path of a file in hard drive.
I have tried using Server.Mappath(fileupload.postedfile.filename) and path.GetFullPath(fileupload.postedfile.filename).
Both are pointing the wrong path. How could I solve this issue?
Try using below code .
Server.MapPath("~/Files") returns an absolute path based on a folder relative to your application
protected void UploadFile(object sender, EventArgs e)
{
string folderPath = Server.MapPath("~/Files/");
//Check whether Directory (Folder) exists.
if (!Directory.Exists(folderPath))
{
//If Directory (Folder) does not exists. Create it.
Directory.CreateDirectory(folderPath);
}
//Save the File to the Directory (Folder).
FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
//Display the success message.
lblMessage.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded.";
}
Check is performed whether the Folder (Directory) exists. If it does not then the Folder (Directory) is created.
Then the uploaded File is saved into the Folder (Directory).
You get the uploaded file as a Stream on the server. you should save it on the disk and then use it (or use the Stream directly).
I have a CHM file and a help menu and I want to add that file to resources but when I added it to resources it does not work.
I tried to add to subfolder in Resources but still no use
void HelpToolStripMenuItemClick(object sender, EventArgs e)
{
string filePath = Directory.GetCurrentDirectory() + "\\Help\\abc.chm";
try
{
//Check if already exists before making
if (!File.Exists(filePath))
{
var data = Properties.Resources.abc.chm;
using (var stream = new FileStream("abc.chm", FileMode.Create))
{
stream.Write(data, 0, data.Count());
stream.Flush();
}
MessageBox.Show("file made");
}
}
catch
{
//May already be opened
}
Help.ShowHelp(this, filePath);
}
I want to work even when this to work even when the setup is installed
on any computer
I would be better if any tells how to embedded in my setup
First of all, add the help file to your project and open the Properties window for that file. In the CopyToOutputDirectory, choose ‘Copy always’ or ‘Copy if newer’.
This will make sure, that when you’re debugging/testing your application, that it will copy the file to the bin folder.
Start your project in Debug mode and check your bin/debug output folder. Do the same for Release mode and output folder. The CHM should reside there and gets included in your deployment.
A sample code snippet for calling CHM's:
private const string sHTMLHelpFileName = "CHM-example.chm";
...
private void button1_Click(object sender, EventArgs e) {
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + #"\" + sHTMLHelpFileName);
}
For download I provide a C# VS2008 Project including the code above and the help files with different help viewer windows (different CHM files for show case only).
I have a website on Server A and it needs to find a directory on Server B.
On my aspx page, I have:
<mb:FileSystemDataSource
ID="fileSystemDataSource" runat="server"
RootPath="\\servername\Folder\Subfolder"
FoldersOnly="true" />
mb is assembly name alias.
On my aspx.cs page, I have:
protected void Page_Load(object sender, EventArgs e)
{
DataTable gridviewSource = DisplayFilesInGridView();
DataRow gridviewRow;
//sets the server path so it does not default to current server
string ServerPath = System.IO.Path.Combine(
"//",
this.fileSystemDataSource.RootPath);
//Get All Folders Or Directories and add in table
DirectoryInfo directory = new DirectoryInfo(Server.MapPath(ServerPath));
DirectoryInfo[] subDirectories = directory.GetDirectories();
}
Well, it throws an error on the Server.MapPath because it cannot find the server.
I am connected to the network.
I looked at IIS, and I am pretty sure that is not the problem. If so, I would really need specifics.
Any help would be appreciated.
string ServerPath = System.IO.Path.Combine(
"//",
this.fileSystemDataSource.RootPath);
Are you sure that "//" is what you're looking for? UNC paths start with "\\". Try running your program in debug mode and see what the actual ServerPath string turns out to be.
So Directory does not like Server.MapPath. I have to hard code the UNC string into the Directory constructor.
In WPF application where I have included some files in resources, I want to execute them on a button click. How do I specify a path in Process.Start().
private void button1_Click_2(object sender, RoutedEventArgs e)
{
Process.Start("test.txt");
}
Or is there any other way?
private void button1_Click_2(object sender, RoutedEventArgs e)
{
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\test.txt";
if (File.Exists(path))
{
Process.Start(new ProcessStartInfo(path));
}
else
{
MessageBox.Show("No file found"+path);
}
I added a message box and it showed No files found. :(
EDIT:
I Tried to check the path after publishing and this what i got.
No File Found With a Path - C:\Users\Administrator\AppData\Local\Apps\2.0... test.txt
Before I published the Application I got a path which id
No File Found at ..project..\bin\Debug\test.txt which is obvious since my Resource file not included there its Under a Resource Folder and not Debug when i add a test file in debug it open without any problem.
Can someone Help throwing some light on this case.
EDIT:
I want to open a file from Resource directory # C:\Users\Administrator\Documents\Visual Studio 2010\Projects\FastFix\FastFix\Resources Which would be included in my project when i am going to publish it is going to run as a standalone application without installation.
use this
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\test.txt";
if (File.Exists(path))
{
Process.Start(new ProcessStartInfo(path));
}
Manually I Have Mapped Network Drive Y:// To My System .Drive is having Manny Folders each Containg Single XMl File having Same as Folder .
Here I am Trying to Read Xml File From Network Location . But It is Giving Exception Directory Not Found . Below Code I am Using For that .
Fname = txtwbs.Text;
DirectoryInfo objDir = new DirectoryInfo("Y:\\");
\\Y:\\
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
_xmlpath = objDir + "\\" + Fname + "\\" + Fname + ".xml";
if (File.Exists(_xmlpath ))
{
reader(_xmlpath);
}
}
Here Fname is Folder Name Also Xml Name .Whatever User Will Enter the Name of File .
Grab this code: http://pastebin.com/RZnydz4Z
Then on the Application_Start of your global.asax put this:
protected void Application_Start(object sender, EventArgs e)
{
Utilities.Network.NetworkDrive nd = new Utilities.Network.NetworkDrive();
nd.MapNetworkDrive(#"\\server\path", "Z:", "myuser", "mypwd");
}
Then just use like a normal network drive, like this:
File.ReadAllText(#"Z:\myfile.txt");
You have this post tagged as both asp.net and asp-classic. From your code example, I'm guessing asp-classic doesn't apply.
If you are running in ASP.Net, the system wouldn't know about the mapped drive you've created. You should use the UNC path instead. If you aren't running the site with Windows Authentication, you'll also need to impersonate someone who has access to the share, as your anonymous user most likely will receive "Access Denied" errors.
Finally, I don't believe you need the DirectoryInfo call - use Path.Combine()
Ideally, you should use the UNC Path to access the files. \\server\share\path\to\file
Identity impersonate has done the trick for me
......
<system.web>
<identity impersonate="true" userName="yourdomain\yourusername"
password="yourpassword" />
......
......
Use this API. http://www.codeproject.com/Articles/6847/Map-Network-Drive-API
It the only way i have managed to do it in code.
Just modify the class, so it fits to you project.
Use a UNC path rather than a network drive and make sure the user running you application has permissions to access the folder and it's contents. "File not found" can mean wrong permissions or the path is just wrong.