Can anyone please suggest a method to open the the files using their
default application in Silverlight application. I am able to get the
full path of the files that I am selecting.
That is for verifying the files before uploading. While using this:
AutomationFactory.CreateObject("WScript.Shell").Run(FileList[_index].filepath);
I get
System.IO.FileNotFoundException
Its not working if the filename contains whitespaces in it.
If you want to open filepaths with spaces you need to add quotes arround your path. Try to use:
"\"" + FILE_PATH + "\""
In your code:
AutomationFactory.CreateObject("WScript.Shell").Run("\"" + FileList[_index].filepath + "\"");
Related
I am new to c# ,Here I'm trying to form a URL to store the image in API itself .
I want to create a folder in the following structure
Images_Folder --> Fruits_Folder--> Seedless_Folder --> Image.jpg
coding :
string imageURL = HttpContext.Current.Server.MapPath("~/Images/");
folderPath = imageURL + formData.RootFolder + formData.TypeFolder + "/";
filePath = (folderPath + postedFile.FileName);
postedFile.SaveAs(filePath);
By using the above code it stores the image in the following structure.
Images-->Fruits_FolderSeedless_Folder --> Image.jpg
While debugging the code I could see the URL format as follows
"D:\Projects\Dot Net\FruitsDisplay\FruitsDisplaySolution\Images\FruitsSeedless/"
Ex: Images-->FruitsSeedless-->Image.jpg
But I want it should be as Images-->Fruits-->Seedless-->Image.jpg
can anyone help me to solve this.
Let OS decide what to use for sub directories, as it might not always be the familiar \ character. Using Path.Combine() method uses the character that is valid in that environment:
folderPath =Path.Combine(imageURL,formData.RootFolder, formData.TypeFolder, postedFile.FileName);
I have a Winforms program that needs to log data points into a .CSV file. It's fairly simple, date/time and a double (the data), and go to the next line.
Here's what I have so far (not working, I get an error saying the file is busy/already open - however, it's empty)
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogFileName = SavePath.Text + "\\LOG\\Seeing-Log-" + TimeNow.ToString("yyyy-MM-dd") + ".csv";
if (!File.Exists(LogFileName))
File.Create(LogFileName);
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
It's that last line that generates the error.
Any idea what I am doing wrong?
thanks
Steve
File.Create returns an open FileStream to the file that's just been created. Either change your code to work with FileStream in both the non-existent and existent file cases, or just close the file after creating it:
if (!File.Exists(LogFileName))
File.Create(LogFileName).Close();
But, of course, if you check the documentation for AppendAllText:
Appends the specified stringto the file, creating the file if it does not already exist.
You'll realise that the above two lines are completely redundant anyway and can be removed:
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
You can even use the free looging tools. Here is one 'log4net'
You can also write the csv file using this. I am assuming currently you are not using logging tool. it will work for you without any code for implementation .
http://element533.blogspot.in/2010/05/writing-to-csv-using-log4net.html
Have a great day!!
Replace
File.Create(LogFileName);
with
File.Create(LogFileName).Close();
see this to create empty file.
The file is locked when you create it. Just update your code to this:
File.Create(LogFileName).Close();
Solution Found.
Thanks to everyone helping me, I found out what the root problem was. The .trl file had nothing to do with it. It was the path being created wrong. I was doing "TRLR" + Path, when it should have been "TRLR" + fileName. This was a stupid error on my part, and I apologize for wasting your time, but I appreciate the help!
I have a zip file given to us by a 3rd party. In this zip files are custom files. These are just text files with a different extension, which I assume is just to frustrate me.
I'm trying to open this files in my C# application, but it keeps throwing the error that the format is not supported.
Since these are just text files, I feel there must be some way for this to happen.
If anyone has any ideas, please let me know.
Code:
using (ZipArchive archive = ZipFile.OpenRead(_trailerName))
{
ZipArchiveEntry entry = archive.GetEntry(tableChanged + ".trl");
Stream ms = entry.Open(); //Here is what's causing the issue.
StreamReader reader = new StreamReader(ms);
string allLinesRead = reader.ReadToEnd();
string[] everyCell = allLinesRead.Split('|');
int numRecords = Convert.ToInt32(everyCell[1]);
int numChanged = getRecordNum(tableChanged);
Console.Write(numRecords + "/" + numChanged + " - " + tableChanged);
if (numChanged != numRecords)
{
_errorMessage += "Records updated do not match trailer. (" + numRecords + "/" + numChanged + ") Please check database. Table affected: " + tableChanged + Environment.NewLine;
}
}
Error:
The given path's format is not supported.
I know this is specific, but I need advice on what steps I can take to resolve this.
Thanks.
The native zip functionality of .NET is frequently lacking in terms of the ability to handle and modify zip files created by applications other than the windows zip tool. While the "zip" file is standardized, you still see a decent amount of variation on file headers and attributes.
I would suggest you look into DotNetZip (Ionic), which is a third party library that has very robust capabilities in terms of creating and opening zip files. I've found it to be much more forgiving and capable than the basic functionality that .NET gives you, and the code to open a zip is extremely similar to what you have.
In windows explorer I am able to just change the file extension of a .cal file to a .cg4 file. When I do it I get a warning; "changing the file extension might render the file useless, do you still wish to change?" or something like that (OS not in english), but if I click "yes" it works.
But trying to do this programmatically with C# doesn't work. I get an error that states: "The given path's format is not supported."
I'm using File.Move for renaming/converting and that's where the error occurs.
File.Move(directory + fileNameWithoutExtension + ".cal", directory + fileNameWithoutExtension + ".cg4");
What can I do?
does this work?
var pathSource = System.IO.Path.Combine(directory, fileNameWithoutExtension, ".cal");
var pathDest = System.IO.Path.Combine(directory, fileNameWithoutExtension, ".cg4");
File.Move(pathSource, pathDest);
if it also throws an error, check if
fileNameWithoutExtension
already contains the data of
directory
I'm actually working on an app that provides the possibility to the users to upload the files they wish. Those files should also be visualizable once uploaded.
In order to do that I'm trying to get the file path with Server.MapPath and a concatenation of other values. The file path is passed as an argument in a window.open javascript function.
My problem is that I do not get any result at all. No window is opened.
Here is my code:
string completeUrl = Server.MapPath(ConfigurationManager.AppSettings["UsersImagesUploadFolder"] + CurrentUserLogin +
#"\\" + ((GridDataItem) e.Item)["Url"].Text);
string radWindowOpen = "<script type='text/javascript'>window.open('" + completeUrl + "')</" + "script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "fileDisplay", radWindowOpen);
I'm probably missing something obvious but I don't see what it is.
Thank you for your answers.
As Damien has pointed out, Server.MapPath is used for server side path mapping. Clients need to see a path underneath your web app.
For example:
Page.ResolveUrl("~/uploads/" + ConfigurationManager.AppSettings["UsersImagesUploadFolder"] ...
Would resolve a to http://mydomain/vroot/uploads/... etc.
As an aside, note also that #"\\" would result in a double backslash, which I don't think you intended.
Either of #"\" or "\\" would result in a single backslash.