Find directory on different server - c#

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.

Related

Running a batch file in relative path?

There is a batch file which I want to run it when I press the button.
My code works fine when I use absolute (full) path. But using relative path causes to occur an exception.
This is my code:
private void button1_Click(object sender, EventArgs e)
{
//x64
System.Diagnostics.Process batchFile_1 = new System.Diagnostics.Process();
batchFile_1.StartInfo.FileName = #"..\myBatchFiles\BAT1\f1.bat";
batchFile_1.StartInfo.WorkingDirectory = #".\myBatchFiles\BAT1";
batchFile_1.Start();
}
and the raised exception:
The system cannot find the file specified.
The directory of the batch file is:
C:\Users\GntS\myProject\bin\x64\Release\myBatchFiles\BAT1\f1.bat
The output .exe file is located in:
C:\Users\GntS\myProject\bin\x64\Release
I searched and none of the results helped me. What is the correct way to run a batch file in relative path?!
The batch file would be relative to the working directory (i.e. f1.bat)
However, your working directory should be an absolute path. It is not guaranteed whichever path is current for your application (may be set in .lnk). In particular it's not the exe path.
Sou you should use the path of your exe file as obtained from AppDomain.CurrentDomain.BaseDirectory (or any other well known method) to build the path of your batch file and/or working directory.
Finally - use Path.Combine to determine a correctly formatted path.
According to JeffRSon`s answer and comments by MaciejLos and KevinGosse my problem was solved as below:
string executingAppPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
batchFile_1.StartInfo.FileName = executingAppPath.Substring(0, executingAppPath.LastIndexOf('\\')) + "\\myBatchFiles\\BAT1\\f1.bat";
batchFile_1.StartInfo.WorkingDirectory = executingAppPath.Substring(0, executingAppPath.LastIndexOf('\\')) + "\\myBatchFiles\\BAT1";
An alternative way is:
string executingAppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
batchFile_1.StartInfo.FileName = executingAppPath.Substring(6) + "\\myBatchFiles\\BAT1\\f1.bat";
batchFile_1.StartInfo.WorkingDirectory = executingAppPath.Substring(6) + "\\myBatchFiles\\BAT1";
I report it here hoping help somebody.

How to locate relative file path when using GetFiles() in C#?

I've found a few related questions but they're not working that well. The image name is a modified GUID like 3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0 but the extension isn't known (.jpg, .gif, ..etc). The GUID will be coming from a gridview so it's not a static string. Below is what I have but I'm having a difficult time getting the path to work correctly.
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(#"/Images");
MessageBox.Show(filePath.ToString());
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");
Keep getting issues with the directory being invalid. Currently the files are stored on my c: drive.
How can I get the relative path without hardcoding it in? I was using DirectoryInfo(Server.MapPath("Images")); which worked temporarily then started giving this error System.ArgumentException: Second path fragment must not be a drive or UNC name. which seems to be from the path having the drive "C:" This doesn't seem to be a permanent solution once the site is launched though.
The actual path is C:\Website\Name\Images\3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0.jpg
Thanks!
You've used filePath as the first parameter to GetFiles, just use the wildcard and invoke the overload of GetFiles with one parameter.
filePath.GetFiles("_0.*");
The problem is that you are getting DirectoryInfo for "C:\Images".
You want to use Server.MapPath to get the physical path to the folder that is in your website (which could be anywhere on any drive).
Using the ~ means to start from the root of the running website.
So this should do the trick:
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(Server.MapPath("~/Images"));
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");

asp.net 404 - File or directory not found on website but locally it can

I have a folder "res/resx/" which contatins .resx files. What I want is to get all those .resx files.
Here is my code for that.
var Path = Server.MapPath("~/");
var SampleUrl = System.IO.Path.GetFullPath(System.IO.Path.Combine(Path, "Res/resx/"));
string[] files= System.IO.Directory.GetFiles(SampleUrl);
var AllFiles = new System.Collections.ObjectModel.ReadOnlyCollection<string>(files);
foreach (string sFileName in AllFiles)
{
Response.write(sFileName + " <br>");
}
This code is working on my local and i was able to see a list of my resx files. But when i deploy this to my website and access it, an error occurs on the 2nd line of code which says:
Could not find a part of the path
'D:\Websites\mywebsite.com\Res\resx'
I tried allowing directory browsing to see if my files exist. In my local, i can browse the files but on the website, I cannot. And it seems the system cannot find the folder "Res/Resx" too. it says:
404 - File or directory not found. The resource you are looking for
might have been removed, had its name changed, or is temporarily
unavailable.
But the folder exist and it is running on my local. Any advice as to what i should do or is their something i have missed? Thanks
Try this
string[] filePaths = Directory.GetFiles(Server.MapPath("Your folder"));
Look at the System.IO.Directory class and the static method GetFiles. It has an overload that accepts a path and a search pattern. Example:
string[] files = System.IO.Directory.GetFiles(path, "*.resx");
answered by
Anthony Pegram here.

Server.Mappath error

iam trying to fill dropdown box feteching folder name from server but this code showing error.its working in local .but not working in server.can any one help on this
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(#"~\\*.***.***.**\Flextronics\Common\Surendra"));
// DirectoryInfo dirInfo = new DirectoryInfo("D:\\New Folder");
ddlModel.DataSource = dirInfo.GetDirectories();
ddlModel.DataBind();
Server.MapPath(#"~\\*.***.***.**\Flex... return virtual path and it works for you on local because you have physical path "D:\...."
So you will have to use Request.MapPath("~/....."); because it Maps the specified virtual path to a physical path.
the ~ symbol looks to the parent folder of your code file on your local machine
To access a UNC on a network drive, you need something similar to:
Server.MapPath("\\\\servername\\folder\\desiredfile.ext");
The "\\\\" escapes the characters and you need to do that to navigate correctly.
Directory does not like Server.MapPath. Hard code it in:
...new DirectoryInfo("stringUNCtoLoadFilesFrom");

How to Access Shared Network drive File using C# code

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.

Categories

Resources