Monday, August 16, 2010

error C2471: cannot update program database

Title: You may receive a "PRJ0008" or "C2471" or "C1083" or "D8022" or "LNK1103" or similar error message when you try to build a solution in Visual C++


Symptoms:

D8022 : Cannot open 'RSP00000215921192.rsp'
PRJ0008 : Could not delete file 'vc90.idb'.
C1083 : Cannot open program database file 'vc90.pdb'
C2471 : Cannot update program database 'vc90.pdb'
LNK1103 : debugging information corrupt.

Cause:

This problem occurs when all of the following conditions are true:

You have a solution with more than one project in it.
Two or more of the projects are not dependent on each other.
You have parallel builds enabled. (Tools -> Options: Projects and Solutions, Build and Run: "maximum number of parallel project builds" is set to a value greater than 1)
You are building on a system with multiple CPUs (cores).
Two or more of the non-dependent projects are configured to use the same Intermediate and/or Output directory.
A specific race condition in mspdbsrv.exe remains uncorrected.


Resolution:

To resolve the problem do one or more of the following:

1. delete the PDB file from the project folder and try to build again and check

2. Clean the project and Rebuild the solution to solve it.

 Otherwise try to do the below steps

Reconfigure the non-dependent projects to specify an Intermediate and Output directory that is different from one another, e.g. Output Directory = "$(SolutionDir)$(ProjectName)\$(ConfigurationName)", Intermediate Directory = "$(OutDir)".
Adjust your solution's project dependencies (Project -> Project Dependencies...) so that each is dependent on another.
Disable parallel builds.
Add the "/onecpu" boot option to your boot.ini file.
Change you BIOS settings to enable/use only one CPU.
File a problem report with Microsoft Technical Support and keep bugging the crap out of them until they eventually fix mspdbsrv.


Status:

The problem is a combination of both a user project configuration error as well as a race condition in Microsoft's "mspdbsrv.exe" utility that does not properly more than one thread calling it at the same time for the same file resulting in the file's HANDLE being left open.

Additionally Visual Studio itself and/or its build system (VCBUILD and/or MSBUILD) (or all three!) should be made smart enough to detect and alert the user of such user errors so that corrective action can be taken.

This problem has been around for a LOOOOOONG time.


Applies to:

Microsoft Visual C++ 2005
Microsoft Visual C++ 2008
Others?

Saturday, April 17, 2010

Generate GUID C#

System.Guid guid = System.Guid.NewGuid() ;
String id = guid.ToString();

Find IP Address in Simple way C#

string strHostName = System.Net.Dns.GetHostName();// Get Host name.

string myIP = System.Net.Dns.GetHostByName(strHostName).AddressList[0].ToString();

Tuesday, April 13, 2010

Create Directory FTP C#

private void MakeDir(string dirName)
{
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();

ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

FTP upload

private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;

// Create FtpWebRequest object from the Uri provided
// reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

reqFTP = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));//.Create(sURI);//"new Uri("\\AGD14\\Archival")");

// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;

// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

// Specify the data transfer type.
reqFTP.UseBinary = true;

// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;

// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();

try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();

// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);

// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}

// Close the file stream and the Request Stream
strm.Close();
fs.Close();
MessageBox.Show("File Uploaded");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}

loop through folders or directories including sub folders

public static void ProcessDir(string sourceDir/*, int recursionLvl*/)
{

string sTemp = sDestinationFolder;
// sw8.WriteLine("**********" + sourceDir + "***********");

// if (recursionLvl <= 5)
// {
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
sTemp = fileName.Remove(0, sDestinationFolder.Length + 1);
lstSource.Add(sTemp);
WriteLogFile(sTemp, MESSAGE_TYPE.INFO);
// do something with fileName
Console.WriteLine(fileName);
}

// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(sourceDir);
foreach (string subdir in subdirEntries)
{
sTemp = subdir.Remove(0, sDestinationFolder.Length + 1);
WriteLogFile(sTemp, MESSAGE_TYPE.INFO);
lstSource.Add(sTemp);

// Do not iterate through reparse points
if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=
FileAttributes.ReparsePoint)

ProcessDir(subdir/*, recursionLvl + 1*/);
}
// }
}

Loop through FTP folder tree

public static void ProcessFiles(string serverUri, string folder)
{
string targetDir = targetFolder + @"\" + folder;

if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);

FtpWebRequest reqFilename = (FtpWebRequest)WebRequest.Create(serverUri);
reqFilename.Credentials = nc;
reqFilename.Method = WebRequestMethods.Ftp.ListDirectory;

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Credentials = nc;
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

try
{

//FtpWebResponse resFilename = (FtpWebResponse)resFilename.GetResponse();
FtpWebResponse resFilename = (FtpWebResponse)reqFilename.GetResponse();
StreamReader file = new StreamReader(resFilename.GetResponseStream());

string sfileLine;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader dir = new StreamReader(response.GetResponseStream());
List subdir = new List();
Regex rgdir = new Regex("");
//Regex rgfile = new Regex(@"(\b\S*)$");
Regex rgfile = new Regex(@"(\b*)$");
string aline;
string filename;
while (!dir.EndOfStream)
{
aline = dir.ReadLine();
sfileLine = file.ReadLine();
// filename = aline.Substring(aline.LastIndexOf("DIR") + 4).Trim();//rgfile.Match(aline).Groups[0].Value.Trim();
if (rgdir.Match(aline).Success)
{
filename = sfileLine;//aline.Substring(aline.LastIndexOf("DIR") + 4).Trim();//rgfile.Match(aline).Groups[0].Value.Trim();
subdir.Add(filename);
}
else
{
filename = sfileLine;//rgfile.Match(aline).Groups[0].Value.Trim();
}

// WriteLogFile(filename, MESSAGE_TYPE.INFO);
if (folder != null)
{
lstDest.Add(folder + @"\" + filename);
WriteLogFile(folder + @"\" + filename, MESSAGE_TYPE.INFO);
}
else
{
lstDest.Add(filename);
WriteLogFile(filename, MESSAGE_TYPE.INFO);
}
/* if (filename.Substring(filename.Length - 4).ToUpper() == ".ZIP")
{
// Console.WriteLine(folder + @"\" + filename);
WriteLogFile(folder + @"\" + filename, MESSAGE_TYPE.INFO);
// DownloadOneFile(serverUri + "/" + filename, targetDir + @"\" + filename);
// DeleteOneFile(serverUri + "/" + filename);
}*/
}
string sFolderName;
foreach (string s in subdir)
{

if(folder != null)
sFolderName = folder + @"\" + s;
else
sFolderName = s;

//ProcessFiles(serverUri + "/" + s, folder + @"\" + s);

ProcessFiles(serverUri + "/" + s, sFolderName);
}
resFilename.Close();
response.Close();
dir.Close();

}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
}

File Size using FileInfo

 FileInfo f = new FileInfo(fileName);
long s1 = f.Length;

FTP file size

static NetworkCredential nc = new NetworkCredential("user", "password");

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri("ftp://" + ftpServerIP + "/" + sSource));

//(FtpWebRequest)WebRequest.Create(("ftp://ftp.domain.com/doesntexist.txt");
request.Credentials = nc;
request.Method = WebRequestMethods.Ftp.GetFileSize;

Monday, April 5, 2010

what's is the difference between reference and pointers?

File Compress C#

using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and already compressed files.
if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".zip")
{
// Create the compressed file.
using (FileStream outFile = File.Create(fi.FullName + ".zip"))
{

using (GZipStream Compress = new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into the compression stream.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
{
Compress.Write(buffer, 0, numRead);
}
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}